2

I have a very simple structure that looks like this:

struct Arr
{
    static constexpr auto width = 2;
    static constexpr auto height = 2;
    std::array<std::array<int, width>, height> values;
};

How do I properly initialize this structure without getting compiler errors or warnings? I tried the following (see example on Compiler Explorer):

auto arr = Arr{1, 2, 3, 4}; // clang: warning: suggest braces around initialization of subobject [-Wmissing-braces]
auto arr = Arr{{1, 2}, {3, 4}}; // clang: error: excess elements in struct initializer
auto arr = Arr{{{1, 2}, {3, 4}}}; // clang: error: excess elements in struct initializer

I know there have been similar questions asked (e.g. Nested aggregate initialization of std::array), but it seems all of them focus on C++11/14.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Krzysiek Karbowiak
  • 1,655
  • 1
  • 9
  • 17

0 Answers0