Saturday, January 18, 2014

template arguments vs template parameters

Template Arguments versus Template Parameters : templates tutorial ccplusplus.com
Compare the following class template


with a similar plain class:


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


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:


It is essential to distinguish between template parameters and template arguments. In short, you can say that you "pass arguments to become parameters."(In the academic world, arguments are sometimes called actual parameters whereas parameters are called formal parameters.) Or more precicely 
  • 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:



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.








-----------------------------------------------------------------
See Also:
-----------------------------------------------------------------

-----------------------------------------------------------------

No comments:

Post a Comment