1

The very straightforward question is: why does the following code not compile?

//foo.cpp
#include <array>
int main() {
  std::array<std::array<int, 2>, 2> a = {{0,1},{2,3}};
  return 0;
}

The error I get (gcc-4.9.2) is

foo.cpp: In function ‘int main()’:
foo.cpp:4:55: error: too many initializers for ‘std::array<std::array<int, 2ul>, 2ul>’
     std::array<std::array<int, 2>, 2> a = {{0,1},{2,3}};

This question addresses the same error but it does not ask for (and replies do not provide) an explanation why this syntax is not accepted.

6
  • Possible duplicate of How do you initialize (trough initializer lists) a multidimensional std::array in C++11? Commented Mar 8, 2016 at 17:25
  • that question addresses the same issue but only asks for the correct syntax. I'd like to know ''why'' this syntax (which would seem the most natural thing to do) does not work. Commented Mar 8, 2016 at 17:27
  • 2
    Take a look at stackoverflow.com/questions/12844475/… Commented Mar 8, 2016 at 19:32
  • @George that's it thank you! Commented Mar 8, 2016 at 20:48
  • array is tricky because it sometimes needs extra {}. and how strictly depends on the compiler! Commented Oct 1, 2018 at 22:27

0

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.