Explicit instantiation (C++ only)

You can explicitly tell the compiler when it should generate a definition from a template. This is called explicit instantiation.

Read syntax diagramSkip visual syntax diagramExplicit instantiation declaration syntax
 
>>-template--template_declaration------------------------------><
 

The following are examples of explicit instantiations:

template<class T> class Array { void mf(); };
template class Array<char>;      // explicit instantiation
template void Array<int>::mf();  // explicit instantiation

template<class T> void sort(Array<T>& v) { }
template void sort(Array<char>&); // explicit instantiation

namespace N {
   template<class T> void f(T&) { }
}

template void N::f<int>(int&);
// The explicit instantiation is in namespace N.

int* p = 0;
template<class T> T g(T = &p);
template char g(char);                  // explicit instantiation

template <class T> class X {
   private:
      T v(T arg) { return arg; };
};

template int X<int>::v(int);    // explicit instantiation

template<class T> T g(T val) { return val;}
template<class T> void Array<T>::mf() { }

A template declaration must be in scope at the point of instantiation of the template's explicit instantiation. An explicit instantiation of a template specialization is in the same namespace where you defined the template.

Access checking rules do not apply to names in explicit instantiations. Template arguments and names in a declaration of an explicit instantiation may be private types or objects. In the above example, the compiler allows the explicit instantiation template int X<int>::v(int) even though the member function is declared private.

The compiler does not use default arguments when you explicitly instantiate a template. In the above example the compiler allows the explicit instantiation template char g(char) even though the default argument is an address of type int.

IBM extension

An extern-qualified template declaration does not instantiate the class or function. For both classes and functions, the extern template instantiation prevents the instantiation of parts of the template, provided that the instantiation has not already been triggered by code prior to the extern template instantiation. For classes, the members (both static and nonstatic) are not instantiated. The class itself is instantiated if required to map the class. For functions, the prototype is instantiated, but the body of the template function is not instantiated.

The following examples show template instantiation using extern:

template<class T>class C {
   static int i;
   void f(T) { }
};
template<class U>int C<U>::i = 0;
extern template C<int>;  // extern explicit template instantiation
C<int>c;   // does not cause instantiation of C<int>::i
           // or C<int>::f(int) in this file,
           // but the class is instantiated for mapping
C<char>d;  // normal instantiations
template<class C> C foo(C c) { return c; }
extern template int foo<int>(int);  // extern explicit template instantiation
int i = foo(1);  // does not cause instantiation of the body of foo<int>
End of IBM extension


[ Top of Page | Previous Page | Next Page | Contents | Index ]