是的,它是一个非类型参数。 您可以拥有多种模板参数
类型参数。类型
模板(只有类和别名模板,没有函数或变量模板)
非类型参数指针
参考
积分常量表达式
你拥有的是最后一种。 它是一个编译时常量(所谓的常量表达式),是整数或枚举类型。 在标准中查找之后,我不得不将类模板移动到类型部分 - 即使模板不是类型。 但它们被称为类型参数,目的是为了描述这些类型。 您可以拥有指针(以及成员指针)和对具有外部链接的对象/函数的引用(可以链接到其他目标文件并且其地址在整个程序中是唯一的)。 例子:
模板类型参数:
template
struct Container {
T t;
};
// pass type "long" as argument.
Container test;
模板整数参数:
template
struct Vector {
unsigned char bytes[S];
};
// pass 3 as argument.
Vector<3> test;
模板指针参数(将指针传递给函数)
template
struct FunctionWrapper {
static void call_it() { F(); }
};
// pass address of function do_it as argument.
void do_it() { }
FunctionWrapper test;
模板引用参数(传递整数)
template
struct SillyExample {
static void do_it() { A = 10; }
};
// pass flag as argument
int flag;
SillyExample test;
模板模板参数。
template class AllocatePolicy>
struct Pool {
void allocate(size_t n) {
int *p = AllocatePolicy::allocate(n);
}
};
// pass the template "allocator" as argument.
template
struct allocator { static T * allocate(size_t n) { return 0; } };
Pool test;
不能使用没有任何参数的模板。 但是没有任何显式参数的模板是可能的 - 它有默认参数:
template
struct Vector {
unsigned char buffer[SIZE];
};
Vector<> test;
从语法上讲,保留template<>来标记显式模板特化,而不是没有参数的模板:
template<>
struct Vector<3> {
// alternative definition for SIZE == 3
};