I have a class containing a template method with a non-type template parameter. The code size got really big, so I tried avoiding the inlining by putting it into the .cpp file. But I can only manage to instantiate it explictly for each non-type parameter.
Is an implicit instantiation possible? What would it look like? In an开发者_StackOverflow other related question this link http://www.parashift.com/c++-faq-lite/templates.html is provided but I can't find a solution for implicit instantiation (if there is something like this)...
class Example
{
public:
template<enumExample T_ENUM> void Foo(void);
};
I get linker errors for Foo (unresolved external symbol) when using it.
Your problem is that the template code needs to be visible at the point at which it is instantiated. See C++ FAQ 35.13
Which basically means you can't do what you are trying to. There is an export keyword that makes this possible, but it is very poorly supported and I believe has been removed from the standard in C++0x. See C++ FAQ 35.14 for more information.
For implicit instantiation, the compiler needs to see the implementation of the function template. Usually this means that the implementation needs to be in a header file. If you just want to avoid inlining, you could try writing the implementation of the function template in the header, but outside of the class declaration (although I'm not sure inlining is your real problem).
To reduce code size you can try to reduce the dependencies by implementing, when appropriate, the pimpl idiom.
精彩评论