I would like to understand how the constructor initialization list works with array members.

Assume:

class Type { public: Type() : data{} {} private: uint8_t data[1024]; }; 

Do I understand correctly that Type() : data{} initializes all the elements of the data array to zero?

If so, is it possible to initialize all the elements of an array member with nonzero (identical) values ​​in the constructor initialization list?

Option:

 Type() : data{7} {} 

Causes only the first element of the array member to be initialized.

    2 answers 2

    As part of the uniform initialization concept, arrays in the constructor initialization list are initialized in the same way as elsewhere. There are no features in the initialization list of the constructor, except for some syntactic differences.

    In standard C ++, there is no ready-made syntax for initializing all elements of a regular array with the same [non-zero] value. However, you can dodge through patterns, for example, like this

     class Type { public: Type() : Type(std::make_index_sequence<1024>()) {} private: template <std::size_t ...I> Type(std::index_sequence<I...>) : data{ (I, 7)... } {} int data[1024]; }; 

      In fact, this initialization construct

       data{7} 

      equivalent construction

       uint8_t data[1024] = { 7 }; 

      The initialization list contains only one element that is used to initialize the first element of the array. All other elements of the array do not have corresponding initializers, and therefore 0 will be initialized.