C++11 class 成员初始化

C++11 class member initialization

刚从 C++03 切换到 C++11,我想知道,下面是否定义为始终零初始化所有元素的数组 data

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

是的,这是有保证的; list initialization turns to aggregate initialization 数组类型:

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

然后进行聚合初始化:

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).

所以data的所有元素最后都会是value initialized, for uint32 they'll be zero-initialized

otherwise, the object is zero-initialized.