I'm wondering that std::array initialize all fields to zero
struct Foo {
int a;
int b;
std::uint32_t c : 16;
std::uint32_t d : 16;
};
class Bar {
public:
std::array<Foo, 2> foo;
}
foo's all fields are initialized with zero?
std::array
is an aggregate class. When you default initialise an aggregate, the members of the aggregate are also default initialised. Default initialising an integer does not zero initialise it. If you value initialise the aggregate, then the members of the aggregate are also value initialised. Value initialisation of an integer is zero initialisation.
In short: No.
No. You should do this:
class Bar {
public:
std::array<Foo, 2> foo = {};
}
Or
class Bar {
public:
std::array<Foo, 2> foo = std::array<Foo, 2>();
}