[root@10 c++]# g++ template.cpp
template.cpp: In function ‘int main()’:
template.cpp:25:13: error: the value of ‘n’ is not usable in a constant expression
Alloc
^
template<class T, int n>
struct Alloc { };
int main()
{
int n =3;
Alloc<int, n> A;
这个错误的意思是说:n不是一个可用的常量表达式;
根本原因在于: 整个模板模式编程的实现在于编译器在编译器实现,凡是用到非编译器确定的类型,值的时候都会有问题。也就是模板Alloc第二个参数,参数n,需要是一个常量。
template<class T, int n>
struct Alloc { };
int main()
{
int n =3;
Alloc<int, 13> A;