Sign in to follow this  
brwarner

Templated Function in Classes (In Libs)

Recommended Posts

brwarner    100
I have a regular class with a templated function inside it. This class is inside a static library and I want to use this class outside my library. Everytime I try to use it I get a LNK2019 that the function is not implemented. I'm assuming this is because the function was never generated inside the library because it was never used and, I wanted to know if there is any way to get this to work without making the library limit the template parameters by precreated some of them I need.

Share this post


Link to post
Share on other sites
SiCrane    11839
Unless your compiler supports the export keyword, and seeing as that looks like a MSVC error code, I'm willing to bet that yours doesn't, then you can't put the definition of a template in a separate source file without explicit instantiation for specific types. Without explicit instantiation, the complete definition of the template needs to be available at point of instantiation, which means, in effect, that the definition needs to go into the header. (Or an inline file of some sort, etc.)

Share this post


Link to post
Share on other sites
skalco6    181
you could try to use explicit instantiation
i know it works for class templates and function templates, but i'm not sure about a templated member function of a regular class

maybe this could work... try it
template void MyClass::myMethod<SomeSpecificType>(const SomeSpecificType& arg);

Share this post


Link to post
Share on other sites
brwarner    100
I think I'll put it in the header to keep it flexible. But how does the STL library do it, they have tons of templated classes, do they all stay in the headers?

Share this post


Link to post
Share on other sites
emeyex    382
Quote:
But how does the STL library do it, they have tons of templated classes, do they all stay in the headers?

Yep, all defined in headers.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this