简体   繁体   English

具有非静态成员初始值设定项的类的C ++ 11聚合初始化

[英]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 . 修订版N365320135月通过。

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

See it working live . 见它的工作现场

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 非静态数据成员初始值设定项仅适用于 -std=c++11 或 -std=gnu++11 - Non-static data member initializers only available with -std=c++11 or -std=gnu++11 抱歉,未实现:使用C ++ 11的非静态数据成员初始化器 - Sorry, unimplemented: non-static data member initializers with C++11 C ++ 11非静态成员初始值设定项和已删除的复制构造函数 - C++11 non-static member initializers and deleted copy constructor C ++ 11非静态数据成员统一初始化失败,指针指向同一基类的其他类 - C++11 non-static data member uniform initialization fails for pointers to other classes of same base class Clang ++非静态数据成员初始化错误? C ++ 11 - Clang++ non-static data member initialization error? C++11 为什么以下非静态数据成员初始化在C ++ 11中无效 - Why the following non-static data member initialization is invalid in C++11 警告:非静态数据成员初始值设定项 - c ++ - Warning: non-static data member initializers - c++ C ++:未实现:非静态数据成员初始化器 - C++: unimplemented: non-static data member initializers 部分聚合初始化和非静态数据成员初始化程序 - Partial Aggregate Initialization and Non-static Data Member Initializer C ++ 11:在类构造函数中使用非静态成员函数作为默认参数 - C++11: Use non-static member function as default argument in class constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM