Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Pragmas to control template instantiation

&pagelevel(4)&pagelevel



#pragma { instantiate | do_not_instantiate | can_instantiate } argument


With C++ translations, the instantiation of individual templates or even a group of templates can be controlled with the following pragmas:

  • The instantiate pragma causes the template entity that is specified as an argument to be created. This pragma can be used in all instantiation modes.

  • The do_not_instantiate pragma suppresses the instantiation of the template entity specified as an argument. The typical candidates for this pragma are template entities for which specific definitions (specializations) have been provided. This pragma can be used in all instantiation modes.

  • The can_instantiate pragma is a hint to the compiler that the template entity specified as an argument can, but need not, be created in the compilation unit. This pragma is required in connection with libraries and is only evaluated in automatic instantiation mode.

The following arguments can be specified with the pragmas:

Argument

Examples

a template class name

A<int>

a member function name

A<int>::f

a static data member name

A<int>::i

a member function declaration

void A<int>::f(int, char)

a template function declaration

char * f(int, float)

When a template class name is specified as an argument (e.g. A<int>), the net effect is the same as if the pragma were specified for each member function and for each static data member of that template class. When instantiating an entire class, individual member functions or static data members can be excluded from the instantiation process by using the do_not_instantiate pragma.

Example


#pragma instantiate A<int>
#pragma do_not_instantiate A<int>::f


In order to instantiate templates, the appropriate template definitions must be available in the current compilation unit. If an instantiation is requested explicitly with the instantiate pragma, and no template definition or only a specific definition (specialization) is available, an error message (ERROR) is issued.

Example


template <class T> void f1(T); // no body provided
template <class T> void g1(T); // no body provided
template <> 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 either (due to the missing template definitions), but no error message will be issued during the compilation in this case. The missing template definitions will, however, result in a linker error at link time.

If a member function name (e.g. A<int>::f) is specified as a pragma argument, it must not be an overloaded function. Overloaded member functions can be instantiated by providing the complete member function declaration, as in
#pragma instantiate char * A<int>::f(int, char *)

The argument to an instantiation pragma may not be a compiler-generated function or a pure virtual function.

When an extern inline function or an inline variable is explicitly instantiated, it is assigned to the module containing the instantiation. The extern entry will be placed in this module.