Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safely initializing a std::array of bools

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?

like image 922
anastaciu Avatar asked Oct 28 '25 08:10

anastaciu


1 Answers

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.

like image 159
HolyBlackCat Avatar answered Oct 29 '25 22:10

HolyBlackCat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!