Search code examples
c++stdarrayzero-initializationdefault-initialization

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?


Solution

  • 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.