Templated Function in Classes (In Libs)

Started by
3 comments, last by emeyex 16 years, 2 months ago
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.
Advertisement
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.)
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);
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?
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.

This topic is closed to new replies.

Advertisement