7.5 Template Arguments versus Template Parameters

Ru-Brd

Compare the following class template

 template <typename T, int N>  class ArrayInClass {    public:      T array[N];  }; 

with a similar plain class:

 class DoubleArrayInClass {    public:      double array[10];  }; 

The latter becomes essentially equivalent to the former if we replace the parameters T and N by double and 10 respectively. In C++, the name of this replacement is denoted as

 ArrayInClass<double,10> 

Note how the name of the template is followed by so-called template arguments in angle brackets.

Regardless of whether these arguments are themselves dependent on template parameters, the combination of the template name, followed by the arguments in angle brackets, is called a template-id .

This name can be used much like a corresponding nontemplate entity would be used. For example:

 int main()  {      ArrayInClass<double,10> ad;      ad.array[0] = 1.0;  } 

It is essential to distinguish between template parameters and template arguments . In short, you can say that you "pass arguments to become parameters ." [2] Or more precicely:

[2] In the academic world, arguments are sometimes called actual parameters whereas parameters are called formal parameters .

  • Template parameters are those names that are listed after the keyword template in the template declaration or definition ( T and N in our example).

  • Template arguments are the items that are substituted for template parameters ( double and 10 in our example). Unlike template parameters, template arguments can be more than just "names."

The substitution of template parameters by template arguments is explicit when indicated with a template-id, but there are various situations when the substitution is implicit (for example, if template parameters are substituted by their default arguments).

A fundamental principle is that any template argument must be a quantity or value that can be determined at compile time. As becomes clear later, this requirement translates into dramatic benefits for the run-time costs of template entities. Because template parameters are eventually substituted by compile-time values, they can themselves be used to form compile-time expressions. This was exploited in the ArrayInClass template to size the member array array . The size of an array must be a so-called constant-expression , and the template parameter N qualifies as such.

We can push this reasoning a little further: Because template parameters are compile-time entities, they can also be used to create valid template arguments. Here is an example:

 template <typename T>  class Dozen {    public:      ArrayInClass<T,12> contents;  }; 

Note how in this example the name T is both a template parameter and a template argument. Thus, a mechanism is available to enable the construction of more complex templates from simpler ones. Of course, this is not fundamentally different from the mechanisms that allow us to assemble types and functions.

Ru-Brd


C++ Templates
C++ Templates: The Complete Guide
ISBN: 0201734842
EAN: 2147483647
Year: 2002
Pages: 185

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net