C++ Programming Guide

Function Template Definition

If you declare a template, you must also define it. A definition provides enough information to implement the template. The following example defines the template declared in the previous example.


template <class Number> Number twice( Number original )
    { return original + original; }

Because template definitions often appear in header files, a template definition might be repeated in several compilation units. All definitions, however, must be the same. This restriction is called the One-Definition Rule.

C++ 5.0 does not support non-type template parameters for function templates. For example, the following template is not supported because its argument is an expression instead of a type.


template <int count> void foo( )  // unsupported non-type parameter
{
    int x[count]
    for (int i = 0; i < count; ++i ) 
        // ... do something with x
}

foo<10>(); // call foo with template argument 10; unsupported