How to force template code to be created without instantiating an object?

I have a template class that is valid only for a pair 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 compilation, and also because of its proprietary):

doIt.cpp:

 template <> void doer<T>::doIt() { /* how to do it */ } 

... and use it like this: use.cpp:

 int main( int, char** ) { doer<int>::doIt() } 

The above is not related, because the void doer :: doIt (void) implementation has never been in scope at the place where it was called.

I can get the code to be generated in doItv2.obj as follows:

doIt_v2.cpp:

 template <> void doer<T>::doIt() { /* how to do it */ } doer<int> a; doer<real> b; 

but it causes a lot of headaches (dynamic memory allocation before entering the main one), and I really do not want to make an instance - I just want object code to be created to instantiate the template.

Any ideas?

+4
source share
1 answer

See How to create template source code . I think that you are after the second method described there: an explicit implementation of the template.

+6
source

All Articles