19

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?

0

5 Answers 5

21

It's bool[4][2] You can verify it by 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
Sign up to request clarification or add additional context in comments.

Comments

13

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

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

Then what_am_i is bool[4][2].

Comments

6

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

It's 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) == 2

Comments

3

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

Comments

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

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

1 Comment

or you can cout << typeid(what_am_i).name()—after piping through c++filt, it reports bool [4][2].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.