Initialization by default std :: array?

With C ++ 11 std::array , do I have a guarantee that the syntax is std::array<T, N> x; will be initialized by all array elements by default?

EDIT : if not, is there a syntax that will work on all arrays (including zero-size arrays) to initialize all elements by default?

EDIT : on cppreference , the default constructor description says:

 (constructor) (implicitly declared) (public member function) default-constructs or copy-constructs every element of the array 

so the answer may be yes. But I would like to be sure of this in accordance with the standard or future standard.

+54
initialization c ++ 11 default-constructor stdarray
Aug 18 '13 at 3:07
source share
4 answers

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.

+81
Aug 18 '13 at 4:59 on
source share

Default initialization is a term from the Standard that potentially does not mean any initialization at all, so you probably mean zero initialization.

The description on cppreference.com is actually a bit misleading. std::array is an aggregate class, and if the element type is primitive, it is POD: "plain old data", and the semantics closely correspond to the C language. The implicitly defined constructor std::array< int, N > is trivial, which is absolutely nothing does.

Syntax like std::array< int, 3 >() or std::array< int, 3 > x{} that provide null values ​​do not do this by calling the constructor. Getting zeros is part of the initialization of the value specified in C ++ 11 §8.5 / 8:

To initialize an object of type type T means:

- if T is a class of a class (possibly cv-qualit) without a user-provided or remote default constructor, then the object is initialized to zero ... and if T has a non-trivial default constructor, the default object is -initialized;

std::array does not have a custom default constructor, so it gets zero initialization. It has an implicitly defined default constructor, but it is trivial, so it is not initialized by default. (But that doesn’t matter, since trivial initialization, by definition, does not affect runtime.)

if not, is there a syntax that will work on all arrays (including arrays of zero size) to initialize all elements to their default values?

C-style masks and std::array are both aggregates, and the way to completely zero-initialize any aggregate is with the syntax = {} . This works with C ++ 98. Note that C-style arrays cannot have zero size and that sizeof (std::array< X, 0 >) not equal to zero.

+10
Aug 19 '13 at 0:18
source share

Both T x[N]; and std::array<T, N> x; by default, each element of the array is initialized.

For example, if T = std::string , each element will be an empty string. If T is a class without a default constructor, then both will not compile. If T = int , each element will have an undefined value (if this declaration is not in the namespace area)

+3
Aug 18 '13 at 3:52 on
source share

First of all, T x [N] initializes the default elements, although the default initialization of the scalar type T actually does nothing. The above also applies to std :: array x. I think you need list initialization.

0
Jun 26 '14 at 9:35
source share



All Articles