1

In this piece of code the initialisation of the Foo does not compile, I get the error error: too many initializers for 'std::array<std::array<int, 3>, 3>.

On the other hand the initialisations of FooOK, FooOK1D and Bar compile fine.

#include <array>
#include <vector>

std::array<std::array<int, 3>, 3> Foo
{
  {1,2,3},   // does not compile: error: too many initializers for 'std::array<std::array<int, 3>, 3>'
  {4,5,6},
  {7,8,9}
};

std::array<std::array<int, 3>, 3> FooOK
{
    1,2,3    // compiles fine
};


std::array<int, 3> FooOK1D
{
    1,2,3    // compiles fine
};

std::vector<std::vector<int>> Bar
{
  {1,2,3},   // compiles fine
  {4,5,6},
  {7,8,9}
};

What is the problem here?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 3
    `std::array` is a type with `T[N]` inside, so aggregate initialization (might) need extra `{}` [Demo](https://godbolt.org/z/PEs1r34P1) – Jarod42 Oct 18 '21 at 13:58

0 Answers0