[英]Aggregate initialization of non-POD class types?
Is it legal to aggregate initialize non-POD class types in ISO C++? 在ISO C ++中聚合初始化非POD类类型是否合法?
For example if we have a structure with a single method like this: 例如,如果我们有一个带有单个方法的结构,例如:
struct T
{
operator double();
int a;
int b;
} ;
And we initialize an instance of it: 然后我们初始化它的一个实例:
T tObj { 56, 92 };
using aggregate initialization. 使用聚合初始化。 Is this legal?
这合法吗?
Under Clang 3.7 it compiles fine although in VC++ 15 CTP 3 it doesn't. 在Clang 3.7下,它可以很好地编译,但是在VC ++ 15 CTP 3中则不能。
Any insights on the question and a quote from the standard please? 对这个问题有什么见解,并请引用标准吗?
You can aggregate-initialise any aggregate, whether or not it's POD. 您可以聚合初始化任何聚合,无论它是否是POD。 C++11 defines an aggregate thusly:
C ++ 11因此定义了一个聚合:
[dcl.init.aggr] An aggregate is an array or a class with no user-provided constructors, no brace-or-equal-initializers for non-static data members, no private or protected non-static data members, no base classes, and no virtual functions
[dcl.init.aggr] 聚合是一个数组或一个类,没有用户提供的构造函数,没有用于非静态数据成员的花括号或相等初始化器,没有私有或受保护的非静态数据成员,没有基类,并且没有虚拟功能
and your class meets that description. 并且您的班级符合该描述。
C++14 relaxes the restrictions on aggregates, removing "no brace-or-equal-initializers for non-static data members"; C ++ 14放宽了对聚合的限制,删除了“对于非静态数据成员,不使用括号或相等的初始化程序”; that doesn't affect this question.
不会影响这个问题。
Note that your class is also POD; 请注意,您的课程也是POD; simply having a member function doesn't disqualify it.
仅仅拥有一个成员函数并不能取消其资格。 But being POD is largely unrelated to whether or not it's an aggregate.
但是成为POD在很大程度上与它是否是聚合无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.