C++11 class member initialization

188 views Asked by At

Just switched to C++11 from C++03, and I was wondering, is the following defined to always zero initialize the array data for all elements?

template<size_t COUNT>
class Test {
public:
    uint32 data[COUNT] = {};
};
1

There are 1 answers

0
leslie.yao On BEST ANSWER

Yes it's guaranteed; list initialization turns to aggregate initialization for array type:

Otherwise, if T is an aggregate type, aggregate initialization is performed.

then for aggregate initialization:

If the number of initializer clauses is less than the number of members or initializer list is completely empty, the remaining members are initialized by 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).

So all the elements of data will be value initialized, for uint32 they'll be zero-initialized at last.

otherwise, the object is zero-initialized.