WCHAR wszFoo[CONSTANT_BAR] = {0};
I've never seen something like {0} used in C++ as part of the language. And I have no idea how to search for a question like this online. What is it?
WCHAR wszFoo[CONSTANT_BAR] = {0};
I've never seen something like {0} used in C++ as part of the language. And I have no idea how to search for a question like this online. What is it?
Missing initialization values use zero
If an explicit array size is specified, but an shorter initiliazation list is specified, the unspecified elements are set to zero.
float pressure[10] = {2.101, 2.32, 1.44};This not only initializes the first three values, but all remaining elements are set to 0.0. To initialize an array to all zeros, initialize only the first value.
It's initializing an array.
float p1[1000]; // No intitialization.
float p2[1000] = {0.0}; // All 1000 values initialized to zero.
More here: C++ Notes: Array Initialization
$8.5.1/7 -
"If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly initialized shall be value-initialized (8.5)."
All this means, is that there is an explict request to initialize first element to 0. Since initializers are not specified for the remaining elements, they are value-initialized. This in case of WCHARs means are initialized to 0.
What does value initialized means? Here is what the Standard says in $8.5 (italics are mine)
To value-initialize an object of type T means:
— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized <------ WCHAR will fit here
To zero-initialize an object of type T means:
— if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T; <------ WCHAR will fit here
— if T is a non-union class type, each nonstatic data member and each base-class subobject is zeroinitialized;
— if T is a union type, the object’s first named data member89) is zero-initialized;
— if T is an array type, each element is zero-initialized;
— if T is a reference type, no initialization is performed.