In the following code I'm trying to initialize arrays (raw C-classic and std::array
) minimizing the usage of the element type, i.e. S
:
#include <array>
struct S { unsigned u; int i; };
auto std_array = std::array<S, 3>{
S{0U, 0},
S{1U, 1},
S{2U, 2}
};
S raw_array[] = {
{0U, 0},
{1U, 1},
{2U, 2}
};
/* // compile error: excess elements in struct initializer
std::array<S,3> std_array_no_type = {
{0U, 0},
{1U, 1},
{2U, 2}
};
*/
std::array<S,3> std_array_one_type_only = {
S{0U, 0},
{1U, 1},
{2U, 2}
};
int main() {}
Using the raw_array
I can specify S
only once. But trying the same with std::array
doesn't work (see the commented std_array_no_type
). I have to specify S
type for each or (this is also an interesting part of the question) only for the first element in the initializer list (see std_array_one_type_only
).
So, is there a way to define an initialized std::array
object using the type S
only once? If no, according to which clause of the standard? And why single explicit type S
allows std_array_one_type_only
to be compiled?