我不明白 C++11 大括号初始化规则在这里是如何工作的。 拥有此代码:
struct Position_pod {
int x,y,z;
};
class Position {
public:
Position(int x=0, int y=0, int z=0):x(x),y(y),z(z){}
int x,y,z;
};
struct text_descriptor {
int id;
Position_pod pos;
const int &constNum;
};
struct text_descriptor td[3] = {
{0, {465,223}, 123},
{1, {465,262}, 123},
};
int main()
{
return 0;
}
注意,数组被声明为有 3 个元素,但只提供了 2 个初始化器。
但是它编译没有错误,这听起来很奇怪,因为最后一个数组元素的引用成员将未初始化。确实,它有 NULL 值:
(gdb) p td[2].constNum
$2 = (const int &) @0x0: <error reading variable>
现在是“魔法”:我将 Position_pod 更改为 Position
struct text_descriptor {
int id;
Position_pod pos;
const int &constNum;
};
变成这样:
struct text_descriptor {
int id;
Position pos;
const int &constNum;
};
现在它给出了预期的错误:
error: uninitialized const member ‘text_descriptor::constNum'
我的问题:为什么它在第一种情况下编译,什么时候应该给出错误(如在第二种情况下)。 不同的是,Position_pod 使用 C 风格的大括号初始化,而 Position 使用 C++11 风格的初始化,调用 Position 的构造函数。但这会如何影响不初始化引用成员的可能性呢?
(更新) 编译器: gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
最佳答案
很明显
struct text_descriptor td[3] = { {0, {465,223}, 123}, {1, {465,262}, 123}, };
是列表初始化,并且初始化列表不为空。
C++11 说 ([dcl.init.list]p3):
List-initialization of an object or reference of type
T
is defined as follows:
- If the initializer list has no elements and
T
is a class type with a default constructor, the object is value-initialized.- Otherwise, if
T
is an aggregate, aggregate initialization is performed (8.5.1).- ...
[dcl.init.aggr]p1:
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).
td
是数组,所以是聚合,所以进行聚合初始化。
[dcl.init.aggr]p7:
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 an empty initializer list (8.5.4).
这里就是这种情况,所以 td[2]
是从一个空的初始化列表初始化的,这(再次是 [dcl.init.list]p3)意味着它是值初始化的。
反过来,值初始化意味着 ([dcl.init]p7):
To value-initialize an object of type
T
means:
- if
T
is a (possibly cv-qualified) class type (Clause 9) with a user-provided constructor (12.1), ...- if
T
is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, ifT
's implicitly-declared default constructor is non-trivial, that constructor is called.- ...
你的类 text_descriptor
是一个没有用户提供构造函数的类,所以 td[2]
首先是零初始化,然后调用它的构造函数。
零初始化意味着([dcl.init]p5):
To zero-initialize an object or reference of type T means:
- if
T
is a scalar type (3.9), ...- if
T
is a (possibly cv-qualified) non-union class type, each non-static data member and each base-class subobject is zero-initialized and padding is initialized to zero bits;- if
T
is a (possibly cv-qualified) union type, ...- if
T
is an array type, ...- if
T
is a reference type, no initialization is performed.
无论 text_descriptor
的默认构造函数如何,这都是明确定义的:它只是将非引用成员和子成员初始化为零。
然后调用默认构造函数,如果它是非平凡的。下面是默认构造函数的定义方式([special]p5):
A default constructor for a class
X
is a constructor of classX
that can be called without an argument. If there is no user-declared constructor for classX
, a constructor having no parameters is implicitly declared as defaulted (8.4). An implicitly-declared default constructor is an inline public member of its class. A defaulted default constructor for classX
is defined as deleted if:
- ...
- any non-static data member with no brace-or-equal-initializer is of reference type,
- ...
A default constructor is trivial if it is not user-provided and if:
- its class has no virtual functions (10.3) and no virtual base classes (10.1), and
- no non-static data member of its class has a brace-or-equal-initializer, and
- all the direct base classes of its class have trivial default constructors, and
- for all the non-static data members of its class that are of class type (or array thereof), each such class has a trivial default constructor.
Otherwise, the default constructor is non-trivial.
因此,隐式定义的构造函数被删除,正如预期的那样,但是如果 pos
是 POD 类型(!),它也是微不足道的。因为构造函数是微不足道的,所以它没有被调用。因为构造函数没有被调用,所以被删除是没有问题的。
这是 C++11 中的一个大漏洞,现已修复。碰巧已经修复处理inaccessible trivial default constructors ,但固定的措辞也涵盖已删除的琐碎默认构造函数。 N4140(大约 C++14)在 [dcl.init.aggr]p7(强调我的)中说:
- if
T
is a (possibly cv-qualified) class type without a user-provided or deleted default constructor, then the object is zero-initialized and the semantic constraints for default-initialization are checked, and ifT
has a non-trivial default constructor, the object is default-initialized;
作为 T.C.在评论中指出,another DR也进行了更改,以便 td[2]
仍然从一个空的初始化程序列表进行初始化,但是该空的初始化程序列表现在意味着聚合初始化。反过来,这意味着 td[2]
的每个成员也是从一个空的初始化程序列表初始化的(再次是 [dcl.init.aggr]p7),所以似乎初始化了引用{}
的成员。
[dcl.init.aggr]p9 然后说(正如 remyabel 在现已删除的答案中指出的那样):
If an incomplete or empty initializer-list leaves a member of reference type uninitialized, the program is ill-formed.
我不清楚这是否适用于从隐式 {}
初始化的引用,但编译器确实会这样解释它,而且它没有太多其他含义。
关于C++11奇怪的大括号初始化行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28408911/