如果要声明模板,请先定义该模板。定义提供了实现模板所需的足够信息。以下示例定义了在前一个示例中声明的模板。
template <class Number> Number twice( Number original ) { return original + original; } |
因为模板定义通常出现在头文件中,所以模板定义必须在多个编译单元中重复。不过所有的定义都必须是相同的。该限制称为一次定义规则。
编译器不支持函数参数列表中非类型模板参数的表达式,如以下示例所示。
// Expressions with non-type template parameters // in the function parameter list are not supported template<int I> void foo( mytype<2*I> ) { ... } template<int I, int J> void foo( int a[I+J] ) { ... } |