I would like to be sure that the following
int i{};
double x{};
initialize all the variables to 0. My compiler seems to do that in all modes, but I need to be sure that it is clearly stated by the standard.
Any reference to the C++11 standard is welcome.
If a type has a default constructor, either implicitly or explicitly declared, you can use brace initialization with empty braces to invoke it. For example, the following class may be initialized by using both empty and non-empty brace initialization: C++ Copy.
operator new is not guaranteed to initialize memory to anything, and the new-expression that allocates an unsigned int without a new-initializer leaves the object with an indeterminate value. Reading the value of an uninitialized object results in undefined behavior.
You don't have to initialise every element of a structure, but can initialise only the first one; you don't need nested {} even to initialise aggregate members of a structure. Anything in C can be initialised with = 0 ; this initialises numeric elements to zero and pointers null.
If T is scalar (arithmetic, pointer, enum), it is initialized from 0 ; if it's a class type, all base classes and data members are zero-initialized; if it's an array, each element is zero-initialized.
Yes, this is guaranteed by the standard: this is actually performing value-initialization
.
In particular, see the point 4) on the page: it states that it has to be value-initialization
:
Value initialization is performed in these situations:
...
4) when a named variable (automatic, static, or thread-local) is declared with the initializer consisting of a pair of braces.
And on the same page, you see that the effect of value-initialization
for built-in types is to initialize them with 0 (square braces are mine):
The effects of value initialization are:
...
4) Otherwise [if non-class, non-array type], the object is zero-initialized.
This is stated by the standard (all quotes from N3337).
T x{};
is list-initialization.
[dcl.init.list]/1:
List-initialization is initialization of an object or reference from a braced-init-list.Such an initializer is called an initializer list, and the comma-separated initializer-clauses of the list are called the elements of the initializer list. An initializer list may be empty. [...]
The applicable definition for list-initialization:
[dcl.init.list]/3:
List-initialization of an object or reference of type T is defined as follows:
- [lots of non-applicable rules]
- Otherwise, if the initializer list has no elements, the object is value-initialized.
So that form for built-in types is value-initialization:
[dcl.init]/7:
To value-initialize an object of type T means:
- [non-applicable rules]
- otherwise, the object is zero-initialized.
So now we're looking for zero-initialization (yes, C++ has a lot of types of initialization):
[dcl.init]/5:
To zero-initialize an object or reference of type T means:
- if
T
is a scalar type (3.9), the object is set to the value 0 (zero), taken as an integral constant expression, converted toT;
- [...]
Yay, since arithmetic types are scalar types ([basic.types]/9
if you don't trust me), these forms both initialize their objects with 0
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With