By definition, the default initialization is the initialization that occurs when another initialization is not specified; C ++ ensures that any object for which you do not provide an explicit initializer will be initialized by default (C ++ 11 §8.5 / 11). This includes objects like std::array<T, N> and T[N] .
Keep in mind that there are types for which initialization does not matter by default, and leaves the value of the object undefined: any type of non-class, non-array (§8.5 / 6). Therefore, the default initialized array of objects with these types will have an undefined value, for example:
int plain_int; int c_style_array[13]; std::array<int, 13> cxx_style_array;
Both c-style and std::array are filled with integers of undefined value, just as plain_int has undefined value.
Is there any syntax that will work on all arrays (including arrays of zero size) to initialize all elements to their default values?
I assume that when you say "default", you really mean "initialize all elements to T{} ". This is not the default initialization, this is the initialization of the value (8.5 / 7). You can easily request initialization of values in C ++ 11 by indicating each declaration with an empty initializer:
int plain_int{}; int c_style_array[13]{}; std::array<int, 13> cxx_style_array{};
Which will initialize all elements of the array in turn, as a result we get plain_old_int and all members of both types of arrays are initialized to zero.
Casey Aug 18 '13 at 4:59 on 2013-08-18 04:59
source share