|
|
One approach to controlling instantiation is to use #pragma in a program:
#pragma instantiate A<int> // template class name#pragma instantiate A<int>::f // member function name
#pragma instantiate A<int>::i // static data member name
#pragma instantiate void A<int>::f(int, char) // member function declaration
#pragma instantiate char* f(int, float) // template function declaration
do_not_instantiate can be substituted for instantiate to exclude a specific member when instantiating a whole template class. For example,
#pragma instantiate A<int> #pragma do_not_instantiate A<int>::f
Template definitions must be present in the source file (typically via an included header) for instantiation to occur. If an instantiation is explicitly requested via instantiate and no template definition is available or a specific definition is provided (a specialization), an error will be given.
template <class T> void f1(T); // No body provided template <class T> void g1(T); // No body provided void f1(int) {} // Specific definition int main() { int i; double d; f1(i); f1(d); g1(i); g1(d); } #pragma instantiate void f1(int) // error - specific definition #pragma instantiate void g1(int) // error - no body providedf1(double) and g1(double) will not be instantiated (because no bodies were supplied) but no errors will be produced during the compilation (if no bodies are supplied at link time, a linker error will be produced).
You can specify overloaded functions by giving the complete member function declaration including all argument types. Inline and pure virtual functions cannot be instantiated.