10.2. AIX template implementations

The template mechanism provides a way of defining general container types, such as list, vector, and stack, where the specific type of the elements is left as a parameter. Two types of templates can be defined:

Class templatesSpecify how individual classes can be constructed.
Function templatesSpecify how individual functions can be constructed.

Regardless of the type of template being used, the code is essentially split into three parts:

Template declarationThis is the part of the source code that declares the template class or function. It does not necessarily contain the definition of the class or function, although it may optionally do so. For example, a template declaration may describe a Stack template class, as shown in Example 10-1.
Template definitionThis portion of code is the definition of the template function itself or the template class member functions. For example, using the Stack class template, this portion of code would define the member functions used to manipulate the stack, as shown in Example 10-2 on page 380.
Template instanceThe template instance code is generated by the compiler for each instance of the template. For example, this would be the code to handle a specific instance of the stack template class, such as a stack of integer values.

The difference between the components is that the template declaration must be visible in every compilation unit that uses the template. The template definition and code for each instance of the template need only be visible once in each group of compilation units that are linked together to make a single executable.

Example 10-1. Stack template declaration
template <class T> class stack
{
private:
    T* v;
    T* p;
    int sz;
public:
    stack(int);
    ~stack();
    void push(T);
    T pop();
};

Example 10-2. Stack template member function definition
template <class T> stack<T>::stack(int s)
{
    v = p = new T[sz=s];
}

template <class T> stack<T>::~stack()
{
    delete [] v;
}

template <class T> void stack<T>::push(T a)
{
    *p++ = a;
}

template <class T> T stack<T>::pop()
{
    T ret = *p;
    p--;
    return ret;
}

10.2.1. Generated function bodies

When you use class templates and function templates in your program, the compiler automatically generates function bodies for all template functions that are instantiated. The compiler follows four basic rules to determine when to generate template functions. The compiler applies the rules in the following order:

  1. If a template declares a function to have internal linkage, the function must be defined within the same compilation unit. The compiler generates the function with internal linkage, and it is not shared with other compilation units. This is the case if the template class has in-line member functions.

  2. If a template function is referenced in a compilation unit, but it is not declared to have internal linkage, the compiler looks for a definition of the function in the same compilation unit. If a definition is found, the function is instantiated. If the -qtemplateregistry option is in effect, and the function is already instantiated in another compilation unit, the function is not instantiated in this compilation unit.

  3. If the -qtempinc option is in effect, the compiler creates a template instantiation file when the template function is declared but not defined. The template function is instantiated when the template instantiation file compiles.

  4. If none of the preceding rules applies, the compiler does not generate the definition of the template function. It must be instantiated in another compilation unit.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset