该类型的"类型的数组T
"是T [dimension]
,这是你可以通过什么作为模板参数.例如:
someTemplate<int [10]> t; // array type as template parameter
int a[5]; // array of 5 ints named 'a'
Run Code Online (Sandbox Code Playgroud)
数组必须具有必须大于0的维度U u[];
.这意味着例如非法.
有些情况可能看似异常,第一个是参数:
void f(T[]);
Run Code Online (Sandbox Code Playgroud)
这是参数的特殊规则,f()
实际上等同于以下内容:
void f(T*);
Run Code Online (Sandbox Code Playgroud)
然后是数组的直接inialization:
int a[] = { 1, 2, 3, 4 };
Run Code Online (Sandbox Code Playgroud)
这里,数组大小通过初始化程序中的元素数隐式给出,因此类型为a
is int[4]
.
还有不完整的数组类型没有特定的边界,但是你不能直接创建这些的实例(更多请参阅Johannes的答案):
template<class T> struct X { typedef T type; };
X<int[]>::type a = { 1, 2, 3 };
Run Code Online (Sandbox Code Playgroud)
如果您正在寻找动态数组,请选择标准容器std::vector<T>
.