部分聚合初始化和非静态数据成员初始化 [英] Partial Aggregate Initialization and Non-static Data Member Initializer

查看:89
本文介绍了部分聚合初始化和非静态数据成员初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

struct Point {
  int x = 0;
  int y = 10;
};

Point p = {1,};
p.x == 1;  // true
p.y == 10; // is this true?

根据初始化列表中标准缺少的元素进行值初始化,因此 y 应该为 int() 0 ,但它不会似乎没有说在非静态数据成员初始化程序的情况下会发生什么.

According to the standard missing elements in initializer list are value initialized, so y should be int() or 0, but it doesn't seem to say what happen in the situation of Non-static Data Member Initializer.

根据回答,显然这是无效的c ++ 11,我想知道c ++ 1y的情况.

According to the answer, apparently this is invalid c++11, I would like to know the situation in c++1y.

推荐答案

C ++ 98,C ++ 03

非静态数据成员初始化程序(NSDMI)不存在;这个问题不适用.

C++98, C++03

Non-static data member initialisers (NSDMIs) do not exist; the question is inapplicable.

首先,该初始化无效,因为您的类型不是集合:

Well, first of all, this initialisation is invalid because your type is not an aggregate:

[C ++ 11:8.5.1/1]: 聚集是一个数组或一个一个类(第9条)具有用户提供的构造函数(12.1),对于非静态数据成员,没有括号或相等初始化器 (9.2),没有私有或私有受保护的非静态数据成员(第11条),没有基类(第10章),也没有虚函数(10.3).

[C++11: 8.5.1/1]: An aggregate is an array or a class (Clause 9) with 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).

因此,此处无法执行聚合初始化;使用 std :: initializer_list 的构造函数将是使用该初始化语法( [C ++ 11:8.5.4/3] )的唯一方法,但是您却没有也不是其中之一.

So, aggregate initialisation can't be performed here; a constructor taking an std::initializer_list would be your only way to use that initialisation syntax ([C++11: 8.5.4/3]), but you don't have one of those either.

因此,问题的整个前提都是有缺陷的:不可能使自己陷入这种状态.

Consequently, the entire premise of the question is flawed: it is not possible to get yourself into this state.

在即将发布的标准版本中,放宽了聚合的定义,以使您的类型被视为聚合(只要这两个成员都保持 public >!):

In the upcoming version of the standard, the definition of aggregates has been relaxed to allow your type to be deemed an aggregate (as long as both of those members stay public!):

[n3936:8.5.1/1] 聚合是一个数组或一个一个类(第9条),没有用户提供构造函数(12.1),没有私有或受保护的非静态数据成员(第11章),没有基类(第10章)和虚拟函数(10.3).

[n3936: 8.5.1/1] 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).

接着,有一条规则可以保证您要寻找的结果:

Following on from this, there's a rule that guarantees the result you're looking for:

[n3936:8.5.1/7] :如果列表中的初始化器子句少于集合中的成员,则每个成员都不会明确初始化应从其
brace-or-equal-initializer 初始化,或者,如果没有 brace-or-equal-initializer ,从一个空的初始化列表(8.5.4). [示例:

[n3936: 8.5.1/7]: If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from its brace-or-equal-initializer or, if there is no brace-or-equal-initializer, from an empty initializer list (8.5.4). [ Example:

struct S { int a; const char* b; int c; int d = b[a]; };
S ss = { 1, "asdf" };

1 ss.b "asdf" ss初始化 ss.a .c ,其表达式的值的形式为 int {} (即 0 )和 ss.d 的值为 ss.b [ss.a] (即),并位于

initializes ss.a with 1, ss.b with "asdf", ss.c with the value of an expression of the form int{} (that is, 0), and ss.d with the value of ss.b[ss.a] (that is, ’s’), and in

struct X { int i, j, k = 42; };
X a[] = { 1, 2, 3, 4, 5, 6 };
X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } };

a b 具有相同的值-结束示例]

这篇关于部分聚合初始化和非静态数据成员初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆