At first, let's prove why every element in the below array A should be zero
int A[5] = {0};
How to guaranteed? It involves the below standards:
- It is a Aggregate initialization since it is array type, and its number of initializer clauses is less than the number of members, and the first element is copy-initialized as 0.
Each direct public base, (since C++17) array element, or non-static class member, in order of array subscript/appearance in the class definition, is copy-initialized from the corresponding clause of the initializer list.
- The remaining elements in the array will follow the rule of Aggregate initialization rule, and do value-initialization
If the number of initializer clauses is less than the number of members and bases (since C++17) or initializer list is completely empty, the remaining members and bases (since C++17) are initialized by their default member initializers, if provided in the class definition, and otherwise (since C++14) copy-initialized from empty lists, in accordance with the usual list-initialization rules (which performs value-initialization for non-class types and non-aggregate classes with default constructors, and aggregate initialization for aggregates). If a member of a reference type is one of these remaining members, the program is ill-formed.
- According to Value Initialization, the left 4 elements will routine into 'zero initialization' process
otherwise, the object is zero-initialized.
- According to Zero Initialization, the type of remaining elements is int which is a scaler type, so the left 4 elements will be initialized as 0
If T is a scalar type, the object's initial value is the integral constant zero explicitly converted to T.
- So every element in A should be zero
Proof done!
Finally, let's prove why every element in the below array B should be zero
int B[5] = {};
It is a value-initialization form, according to Value Initialization
In all cases, if the empty pair of braces {} is used and T is an aggregate type, aggregate-initialization is performed instead of value-initialization.
It will go into upper aggregate-initialization #2, and then every element in B array should be zero.
Proof done!