Consider the following C++ code:
struct My_Struct
{
int a;
int b;
};
Now I want to declare a constant std::array of these structures:
Option A:
const std::array<My_Struct,2> my_array =
{
{1,2},
{2,3}
};
Option B:
const std::array<My_Struct,2> my_array =
{
My_Struct(1,2),
My_Struct(2,3)
};
Question: why does option A fail to compile and only option B compiles? What's wrong with nested curly braces when initializing std::array?
It seems to me that compiler should have all necessary information to successfully compile option A, yet it produces "too many initializer values" error.
Meanwhile, nested curly braces work really good when initializing plain arrays:
const My_Struct my_array[] =
{
{1,2},
{2,3}
};
Compiler is MSVC 17.2, C++20 mode on.
I can live with option B, but I would like to learn what exactly am I failing to understand in option A in terms of C++ knowledge.