[英]C++11 aggregate initialization for classes with non-static member initializers
Is it allowed in standard: 是否允许标准:
struct A
{
int a = 3;
int b = 3;
};
A a{0,1}; // ???
Is this class still aggregate? 这个课程仍然是聚合的吗?
clang
accepts this code, but gcc
doesn't. clang
接受此代码,但gcc
没有。
In C++11 having in-class member initializers makes the struct/class not an aggregate — this was changed in C++14, however. 在C ++ 11中,具有类内成员初始值设定项使得struct / class不是聚合 - 但是在C ++ 14中已经改变了。 This is something I found surprising when I first ran into it, the rationale for this restriction is that in-class initializers are pretty similar to a user defined constructor but the counter argument is that no one really expects that adding in-class initializers should make their class/struct a non-aggregate, I sure did not.
当我第一次遇到它时,我发现这是令人惊讶的,这个限制的基本原理是类内初始化器非常类似于用户定义的构造函数,但是计数器参数是没有人真正期望添加类内初始化器应该使他们的类/结构是非聚合的,我肯定没有。
From the draft C++11 standard section 8.5.1
Aggregates ( emphasis mine going forward ): 从草案C ++ 11标准第
8.5.1
节聚合 ( 强调我的未来 ):
An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no brace-or-equal initializers for non-static data members (9.2), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).
聚合是一个数组或类(第9节),没有用户提供的构造函数(12.1), 非静态数据成员 (9.2) 没有大括号或相等的初始值 ,没有私有或受保护的非静态数据成员(子句) 11),没有基类(第10条),也没有虚函数(10.3)。
and in C++14 the same paragraph reads: 在C ++ 14中 ,同一段落如下:
An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no private or protected non-static data members (Clause 11), no base classes (Clause 10), and no virtual functions (10.3).
聚合是一个数组或类(第9条),没有用户提供的构造函数(12.1),没有私有或受保护的非静态数据成员(第11条),没有基类(第10条),没有虚函数(10.3) )。
This change is covered in N3605: Member initializers and aggregates which has the following abstract: N3605中包含此更改:成员初始值设定项和聚合 ,具有以下摘要:
Bjarne Stroustrup and Richard Smith raised an issue about aggregate initialization and member-initializers not working together.
Bjarne Stroustrup和Richard Smith提出了一个关于聚合初始化和成员初始化器无法协同工作的问题。 This paper proposes to fix the issue by adopting Smith's proposed wording that removes a restriction that aggregates can't have member-initializers .
本文提出通过采用史密斯提出的措辞来解决这个问题,该措辞消除了聚合不能拥有成员初始化者的限制 。
This comment basically sums up the reluctance to allowing them to be aggregates: 这个评论基本上总结了不愿意让它们成为聚合:
Aggregates cannot have user-defined constructors and member-initializers are essentially some kind of user-defined constructor (element) (see also Core Defect 886).
聚合不能有用户定义的构造函数, 成员初始化器本质上是某种用户定义的构造函数(元素) (另请参见核心缺陷886)。 I'm not against this extension, but it also has implications on what our model of aggregates actually is.
我并不反对这种扩展,但它也会影响我们的聚合模型实际上是什么。 After acceptance of this extension I would like to know how to teach what an aggregate is.
在接受这个扩展之后, 我想知道如何教授聚合是什么。
The revised version N3653 was adopted in May 2013 . 修订版N3653于2013年5月通过。
Update
更新
emsr points out that G++ 5.0 now supports C++14 aggregates with non-static data member initializers using either std=c++1y
or -std=c++14
: emsr指出G ++ 5.0现在支持使用
std=c++1y
或-std=c++14
非静态数据成员初始化程序的C ++ 14聚合 :
struct A { int i, j = i; };
A a = { 42 }; // a.j is also 42
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.