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.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
blue
  • 2,683
  • 19
  • 29
  • Possible duplicate of [How do you initialize (trough initializer lists) a multidimensional std::array in C++11?](http://stackoverflow.com/questions/9504103/how-do-you-initialize-trough-initializer-lists-a-multidimensional-stdarray-i) – rems4e Mar 08 '16 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. – blue Mar 08 '16 at 17:27
  • 2
    Take a look at http://stackoverflow.com/questions/12844475/why-cant-simple-initialize-with-braces-2d-stdarray – George Sovetov Mar 08 '16 at 19:32
  • @George that's it thank you! – blue Mar 08 '16 at 20:48
  • array is tricky because it sometimes needs extra `{}`. and how strictly depends on the compiler! – alfC Oct 01 '18 at 22:27
  • Possible duplicate of [Why can't simple initialize (with braces) 2D std::array?](https://stackoverflow.com/questions/12844475/why-cant-simple-initialize-with-braces-2d-stdarray) – Davis Herring Oct 05 '18 at 02:00

0 Answers0