Reputation: 15788
I have a template class that is only valid for couple of template parameters:
doIt.h:
// only int and float are valid T
template <typename T>
class doer
{
public:
void doIt();
}
I want to hide the implementation inside the .cpp file (for faster compile and also because its proprietary):
doIt.cpp:
template <>
void doer<T>::doIt() { /* how to do it */ }
... and use it as follows: use.cpp:
int main( int, char** )
{
doer<int>::doIt()
}
The above fails to link because the implementation of void doer::doIt(void) was never in scope at the place where it was called.
I can force the code to be generated into doItv2.obj, as follows:
doIt_v2.cpp:
template <>
void doer<T>::doIt() { /* how to do it */ }
doer<int> a;
doer<real> b;
but this causes a variety of headaches (dynamic memory allocation before main is entered) and I actually don't want to make an instance -- I just want the object code for the template instantiations to be generated.
Any ideas?
Upvotes: 4
Views: 595
Reputation: 24551
See the article How to Organize Template Source Code. I think you are after the second method described there: explicit template instantiation.
Upvotes: 6