我正在测试
静态大小的数组
我正在遇到错误:
我注意到运行时大小的数组中的size变量似乎与类型相关联(而不是我们得到的“类型不匹配'T*'和'int [5]'”而是“类型不匹配'T*'和'int [s]'”),这看起来非常奇怪。
那么问题是什么?
- 为什么在这个运行时大小的数组中没有出现指针衰减? - 用于确定运行时大小的数组大小的变量是运行时大小的数组类型的一部分,还是我误解了错误?
type_traits
头文件中的一些工具,用于新的C++14运行时大小数组,请考虑下面的代码:int g[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
template <typename T> void print(T &t)
{
std::cout << "Type id: " << typeid(T).name() << '\n';
std::cout << "is_array: " << std::is_array<decltype(T)>::value << '\n';
std::cout << "is_pointer: " << std::is_pointer<decltype(T)>::value << '\n';
std::cout << "extent: " << std::extent<decltype(T)>::value << '\n';
}
int main()
{
print(g);
return 0;
}
静态大小的数组
g
返回以下输出:Type id: A11_i
is_array: 1
is_pointer: 0
extent: 11
未混淆的名称 A11_i
,我假设这里的 A 表示数组、11 表示元素数量、int 表示元素类型,所以一切都正确。但对于这段新代码:
void f(std::size_t s)
{
int a[s];
print(a);
}
int main()
{
f(5);
return 0;
}
我正在遇到错误:
In function 'void f(std::size_t)':
error: no matching function for call to 'print(int [s])'
note: candidate is:
note: template<class T> void print(T&)
note: template argument deduction/substitution failed:
note: variable-sized array type 'int [s]' is not a valid template argument
我并没有预料到模板中可以传递size参数,但我预料到会有自动的数组指针衰减。我猜测参数 T &
并不适合这种衰减,所以我尝试改变模板签名为:
template <typename T> void print(T *&t)
类似的结果:
In function 'void f(std::size_t)':
error: no matching function for call to 'print(int [s])'
note: candidate is:
note: template<class T> void print(T*&)
note: template argument deduction/substitution failed:
note: mismatched types 'T*' and 'int [s]'
我注意到运行时大小的数组中的size变量似乎与类型相关联(而不是我们得到的“类型不匹配'T*'和'int [5]'”而是“类型不匹配'T*'和'int [s]'”),这看起来非常奇怪。
那么问题是什么?
- 为什么在这个运行时大小的数组中没有出现指针衰减? - 用于确定运行时大小的数组大小的变量是运行时大小的数组类型的一部分,还是我误解了错误?
int a[s];
时使用了VLA扩展。 - Jarod42T*
,而不是T*&
。后者仅适用于实际指针。 - interjay