Mat*_*ias 3 c++ arrays typedef
我对C++中C风格数组的维度有疑问.在使用声明/ typedef时,使用多个维度时,维度似乎很奇怪.例如:
using A1 = int[23]; //! << A1 = int[23]
using A2 = A1[4]; //! << A2 = int[4][23]
std::cout << std::is_same<int[23][4], A2>::value << std::endl; //false
std::cout << std::is_same<int[4][23], A2>::value << std::endl; //true
Run Code Online (Sandbox Code Playgroud)
我认为A2的类型是int [23] [4]而不是int [4] [23].在下面的代码段中观察到相同的行为:
template<typename T>
struct ArrayTest;
template<typename T, size_t N>
struct ArrayTest<T[N]>
{
using type = T;
};
ArrayTest<int[23][2][45]>::type A3; //! T is int[2][45], N is 23
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我认为类型将是int [23] [2]而不是int [2] [45].有谁知道为什么这样的类型推断?我试图在标准中找到解释,但我想我看起来不够努力.
有谁知道为什么这样的类型推断?
using A2 = A1[4];
Run Code Online (Sandbox Code Playgroud)
A2是一个长度为4的A1对象数组.
using A1 = int[23];
Run Code Online (Sandbox Code Playgroud)
A1是长度为23的数组int.所以类型A2是长度为4的长度为23的数组int,或者int[4][23].