DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Instantiating C++ templates

Pragma interface

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 provided
f1(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.


NOTE: Another pragma, can_instantiate, indicates that a specified entity can be instantiated in the current compilation, but need not be; it is used in conjunction with automatic instantiation, to indicate potential sites for instantiation if the template entity turns out to be required.


Next topic: Instantiation via the command line
Previous topic: Manual instantiation

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004