[英]Dimensions of C-style arrays in typedefs
I have a question about the dimensions of C-style arrays in C++. 我对C ++中C风格数组的维度有疑问。 In using declarations/typedefs the dimensions seems strange when using more than one dimension.
在使用声明/ typedef时,使用多个维度时,维度似乎很奇怪。 For example:
例如:
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
I thought that the type of A2 would be int[23][4] and not int[4][23]. 我认为A2的类型是int [23] [4]而不是int [4] [23]。 The same behaviour is observed is observed in the snippet below:
在下面的代码段中观察到相同的行为:
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
In this example I thought the type would be int[23][2] and not int[2][45]. 在这个例子中,我认为类型将是int [23] [2]而不是int [2] [45]。 Does anyone know why the types are deduced like this?
有谁知道为什么这样的类型推断? I have tried to find an explanation in the standard, but I guess I have not looked hard enough.
我试图在标准中找到解释,但我想我看起来不够努力。
Does anyone know why the types are deduced like this?
有谁知道为什么这样的类型推断?
using A2 = A1[4];
A2
is a length 4 array of A1
objects. A2
是A1
对象的长度为4的数组。
using A1 = int[23];
A1
is a length 23 array of int
. A1
是int
的长度为23的数组。 So the type of A2
is length 4 array of length 23 int
, or int[4][23]
. 所以
A2
的类型是长度为4的长度为23 int
数组,或int[4][23]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.