4

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?

anastaciu
  • 23,467
  • 7
  • 28
  • 53

2 Answers2

5

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.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
-1

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.

VillageTech
  • 1,968
  • 8
  • 18
  • 3
    Default-initialization would be `std::array invalid_params;`. With `{}` it's value-initialziation. – HolyBlackCat May 22 '21 at 11:03
  • @HolyBlackCat To be fair, OP didn't specify in their question whether it is safe to assume that the elements would be false using only this syntax (which it is). – eerorika May 22 '21 at 11:06