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

Instantiating C++ templates

The C++ language has a feature known as a ``template''. A template is a skeleton for defining a set of types or functions. Each type or function is created by combining the template with a set of arguments that are themselves types or values. This combining process is called ``instantiation''.


NOTE: Since templates are descriptions of entities (typically, classes) that are parameterizable according to the types they operate upon, they are sometimes called parameterized types.

For example, consider a Vector template:

   template <class T> class Vector {
   	int cursize;
   	T* ptr;
   	void grow();
   public:
   	Vector();
   	~Vector();
   	T& operator[](int);
   };

This template takes one type argument T, has two private data members cursize and ptr, and one private member function grow. There are also a public constructor and destructor and one function operator[](int).

Suppose that you have some class A and you want to use a vector of class objects in your application. To do this, you might say:

   void f()
   {
   	Vector<A> x;
   	A a, b;
   

x[17] = a; b = x[23]; }

Vector<A> is a ``template class'', a combination of a template with specific argument types. This is an instantiation.


Next topic: The instantiation problem

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