HPlogo HP C++ Programmer's Guide: HP 9000 Series Workstations and Servers > Chapter 1 Overview of HP C++

Templates

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

Version 3.0 of HP C++ adds templates, or parameterized types as they are also called. This section briefly describes templates. For a detailed description, see the "Template Instantiation" sections in the C++ Language System Selected Readings and the "Templates" chapter in the The C++ Programming Language. For additional information on templates, see the HP C++ Online Programmer's Guide.

You can create class templates and function templates. A template defines a group of classes or functions. The template has one or more types as parameters. To use a template you provide the particular types or constant expressions as actual parameters thereby automatically creating a particular object or function.

Class Templates

A class template defines a family of classes. To declare a class template, you use the keyword template followed by the template's formal parameters. Class templates can take parameters that are either types or expressions. You define a template class in terms of those parameters. For example, the following is a class template for a simple stack class. The template has two parameters, the type specifier T and the int parameter size. The keyword class in the < > brackets is required to declare a template's type parameter. The first parameter T is used for the stack element type. The second parameter is used for the maximum size of the stack.

template<class T, int size>
class Stack
{
public:
     Stack(){top=-1;}
     void push(const T& item){thestack[++top]=item;}
     T& pop(){return thestack[top--];}
private:
     T thestack[size];
     int top;
};

The member functions and the member data use the formal parameter type T and the formal parameter size. When you declare an instance of the class Stack, you provide an actual type and a constant expression. The object created uses that type and value in place of T and size, respectively. For example, the following program uses the Stack class template to create a stack of 20 integers by providing the type int and the value 20 in the object declaration:

void main()
{       Stack<int,20> myintstack;
        int i;

        myintstack.push(5);
        myintstack.push(56);
        myintstack.push(980);
        myintstack.push(1234);
        i = myintstack.pop();
}

The compiler automatically substitutes the parameters you specified, in this case int and 20, in place of the template formal parameters. You can create other instances of this template using other built-in types as well as user-defined types.

Function Templates

A function template defines a family of functions. To declare a function template, use the keyword template to define the formal parameters, which are types, then define the function in terms of those types. For example, the following is a function template for a swap function. It simply swaps the values of its two arguments:

template<class T>
void swap(T& val1, T& val2)
{
        T temp=val1;
        val1=val2;
        val2=temp;
}

The argument types to the function template swap are not specified. Instead, the formal parameter, T, is a placeholder for the types. To use the function template to create an actual function instance (a template function), you simply call the function defined by the template and provide actual parameters. A version of the function with those parameter types is automatically created.

For example, the following main program calls the function swap twice, passing int parameters in the first case and float parameters in the second case. The compiler uses the swap template to automatically create two versions, or instances, of swap, one that takes int parameters and one that takes float parameters.

void main()
{       int i=2, j=9;
        swap(i,j);
        
        float f=2.2, g=9.9;
        swap(f,g);
}

Other versions of swap can be created with other types that exchange the values of the given type.

Template Code is Stored in a Repository

When you declare a template, the compiler stores information about the template in a repository. The compiler creates a directory, ptrepository for "parameterized type repository," and stores information about your template there. When you use the template, the compiler automatically instantiates the template using the repository.

CC Options for Templates

You can change the default compiler behavior by using the -pt options. For a complete description of these options see Table 3-1 “The CC Command Options” in Chapter 3 “Compiling and Executing HP C++ Programs” of this manual and the section "Template Instantiation User Guide" in the C++ Language System Selected Readings. Note that the PTOPTS environment variable described in this article is not supported by HP C++. Use CXXOPTS instead. CXXOPTS is described in Chapter 3 “Compiling and Executing HP C++ Programs”.

© Hewlett-Packard Development Company, L.P.