【发布时间】:2016-09-06 21:27:45
【问题描述】:
使用默认值初始化结构很简单:
struct X { int a; int b = 2; };
使用大括号初始化器初始化结构也很简单:
X x = {1, 3};
令人惊讶的是,在我删除默认值之前,初始化代码不会编译。那么,在这种情况下我将如何进行初始化呢?我想保留 X 一个没有 c-tor 的 POD。
【问题讨论】:
-
仅供参考:使用 c++14,代码可以编译。
-
The definition of aggregates was changed to explicitly exclude any class with member initializers; therefore, they are not allowed to use aggregate initialization. C++14 relaxes this restriction allowing aggregate initialization on such types. If the braced init list does not provide a value for that argument, the member initializer takes care of it所以基本上你的代码在没有 C-tor 的 C++11 中是不可能的。仅在 C++14 中