Reputation: 127
I have a question about the dimensions of C-style arrays in C++. In using declarations/typedefs the dimensions seems strange when using more than one dimension. 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]. 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]. 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.
Upvotes: 4
Views: 73
Reputation: 227608
Does anyone know why the types are deduced like this?
using A2 = A1[4];
A2
is a length 4 array of A1
objects.
using A1 = int[23];
A1
is a length 23 array of int
. So the type of A2
is length 4 array of length 23 int
, or int[4][23]
.
Upvotes: 6