Should std :: array elements be reset?

std::array<int,4> myInts; std::array<bool,2> myBools; 

Is it possible to consider the elements myInts and myBools false and 0 or do I fill arrays manually?

+4
source share
1 answer

Elements will be set to random values, but you can make sure that they initialize them as follows:

 std::array<int,4> myInts{}; // all zeroes std::array<bool,2> myBools{}; // all false 

Thus, elements should not be reset, they can be initialized for certain values. You can also initialize elements for different values:

 std::array<int,4> myInts{1,2,33,44}; std::array<bool,2> myBools{true, false}; 

If the list of initializers has fewer elements than the size of the array, then the missing ones are initialized to zero:

 std::array<int,4> myInts{1,2}; // array contains 1,2,0,0 

Note The standard indicates that std::array is an aggregate, and has some code example where it shows that it is implemented as having an array data element. He claims that it is easy to emphasize that the type is cumulative, but if this implementation is used (and GCC uses it), then you will need an extra set of curly braces because you are initializing the array inside std::array :

 std::array<int,4> myInts{ {1,2,33,44} }; 

On the other hand, this case is suitable for aligning curly braces, so an extra set of curly braces is not necessary.

+12
source

All Articles