数组的类型定义

时间:2021-10-28 19:32:51

As the title might look very confusing, let me give you an example:

题目可能看起来很混乱,让我给你们举个例子:

typedef bool foo[2];
typedef foo bar[4];
bar what_am_i;

So, is what_am_i a [4][2] dimensional array as I presume, or a [2][4] dimensional array?

那么,什么是[4][2]维数组,还是[2][4]维数组?

5 个解决方案

#1


21  

It's bool[4][2] You can verify it by static_assert:

它是bool[4][2]你可以用static_assert来验证:

static_assert(std::is_same<decltype(what_am_i), bool[4][2]>::value, "");
static_assert(std::is_same<decltype(what_am_i), bool[2][4]>::value, ""); // failed

#2


13  

foo is an array with 2 elements of type bool, i.e. bool[2].

foo是一个包含bool类型的两个元素的数组,即bool[2]。

bar is an array with 4 elements of type foo, i.e. foo[4], each element is a bool[2].

bar是一个包含4个foo类型元素的数组,即foo[4],每个元素都是bool[2]。

Then what_am_i is bool[4][2].

然后what_am_i bool[4][2]。

#3


6  

In order to complete @Slardar Zhang's C++ answer for C:

为了完成@Slardar Zhang的C+ for C:

It's bool[4][2].

bool[4][2]。

You can verify it by either one of the following:

你可以用以下任何一种方法来验证:

  • sizeof(what_am_i)/sizeof(*what_am_i) == 4
  • sizeof(what_am_i)/ sizeof(* what_am_i)= = 4
  • sizeof(*what_am_i)/sizeof(**what_am_i) == 2
  • sizeof(* what_am_i)/ sizeof(* * what_am_i)= = 2

#4


3  

After inspecting the variable through the debugger, I found that I was right - what_am_i is a [4][2] dimensional array.

通过调试器检查变量之后,我发现我是对的——what_am_i是一个[4][2]维度数组。

#5


1  

When you don't know the type of a variable, one of the easy ways is this trick:

当你不知道变量的类型时,一个简单的方法就是:

 template<class T> struct tag_type{using type=T;};
 template<class T> constexpr tag_type<T> tag{};

then do this:

然后这样做:

 tag_type<void> t = tag<some_type>;

almost every compiler will spit out a reasonably readable "you cannot assign tag_type<full_unpacked_type> to tag_type<void>" or somesuch (presuming your type isn't void that is).

几乎所有的编译器都会发出一个合理可读的“您不能将tag_type 分配给tag_type ”或somesuch(假设您的类型不是void)。

You can also verify you have a good guess with

您还可以验证您的猜测是否正确

tag_type<your_guess> t = tag<some_type>;

sometimes you'll want to generate some_type via decltype(some_expression).

有时您希望通过decltype(some_expression)生成some_type。

#1


21  

It's bool[4][2] You can verify it by static_assert:

它是bool[4][2]你可以用static_assert来验证:

static_assert(std::is_same<decltype(what_am_i), bool[4][2]>::value, "");
static_assert(std::is_same<decltype(what_am_i), bool[2][4]>::value, ""); // failed

#2


13  

foo is an array with 2 elements of type bool, i.e. bool[2].

foo是一个包含bool类型的两个元素的数组,即bool[2]。

bar is an array with 4 elements of type foo, i.e. foo[4], each element is a bool[2].

bar是一个包含4个foo类型元素的数组,即foo[4],每个元素都是bool[2]。

Then what_am_i is bool[4][2].

然后what_am_i bool[4][2]。

#3


6  

In order to complete @Slardar Zhang's C++ answer for C:

为了完成@Slardar Zhang的C+ for C:

It's bool[4][2].

bool[4][2]。

You can verify it by either one of the following:

你可以用以下任何一种方法来验证:

  • sizeof(what_am_i)/sizeof(*what_am_i) == 4
  • sizeof(what_am_i)/ sizeof(* what_am_i)= = 4
  • sizeof(*what_am_i)/sizeof(**what_am_i) == 2
  • sizeof(* what_am_i)/ sizeof(* * what_am_i)= = 2

#4


3  

After inspecting the variable through the debugger, I found that I was right - what_am_i is a [4][2] dimensional array.

通过调试器检查变量之后,我发现我是对的——what_am_i是一个[4][2]维度数组。

#5


1  

When you don't know the type of a variable, one of the easy ways is this trick:

当你不知道变量的类型时,一个简单的方法就是:

 template<class T> struct tag_type{using type=T;};
 template<class T> constexpr tag_type<T> tag{};

then do this:

然后这样做:

 tag_type<void> t = tag<some_type>;

almost every compiler will spit out a reasonably readable "you cannot assign tag_type<full_unpacked_type> to tag_type<void>" or somesuch (presuming your type isn't void that is).

几乎所有的编译器都会发出一个合理可读的“您不能将tag_type 分配给tag_type ”或somesuch(假设您的类型不是void)。

You can also verify you have a good guess with

您还可以验证您的猜测是否正确

tag_type<your_guess> t = tag<some_type>;

sometimes you'll want to generate some_type via decltype(some_expression).

有时您希望通过decltype(some_expression)生成some_type。