Safely initializing a std::array of bools

1.1k views Asked by At

Given this array declaration and initialization:

std::array<bool, 20> invalid_params{};

Can I assume that all the elements in the array will always be initialized to false, or is it better to do it explicitly?

2

There are 2 answers

1
HolyBlackCat On BEST ANSWER

It's guaranteed to be filled with false values.

std::array is an aggregate, hence it has no user-defined constructors, hence value-initializing it performs zero-initialization.


But if you remove {} (turning it into default-initialization), the elements will be uninitialized.

2
VillageTech On

Documentation says: "default initialization may result in indeterminate values for non-class T". So, it seems that you can not assume that it will be initialized.