简体   繁体   中英

Dimensions of C-style arrays in typedefs

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.

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] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM