gpt4 book ai didi

c++ - 为什么它打破了初始化列表的顺序规则时会起作用

转载 作者:IT老高 更新时间:2023-10-28 21:45:51 25 4
gpt4 key购买 nike

为什么这段代码有效?我预计这会因为违反基本 C++ 规则之一而失败:

#include <iostream>
using namespace std;

struct A {
A() { cout << "ctor A" << endl; }
void doSth() { cout << "a doing sth" << endl; }
};

struct B {
B(A& a) : a(a) { cout << "ctor B" << endl; }

void doSth() { a.doSth(); }

A& a;
};

struct C {
C() : b(a) { cout << "ctor C" << endl; }

void doSth() { b.doSth(); }

B b;
A a;
};

int main()
{
C c;
c.doSth();
}

https://wandbox.org/permlink/aoJsYkbhDO6pNrg0

我预计这会失败,因为在 C 的构造函数中,当尚未创建此 A 对象时,B 会获得对 A 对象的引用。

我错过了什么吗?初始化顺序规则是否与字段顺序相同不适用于引用?

编辑:更让我吃惊的是,我可以添加对“a.doSth();”的调用。在 B 构造函数中,这也将起作用。为什么?此时 A 对象不应该存在!

最佳答案

只要 B 的构造函数不使用 引用它获取的任何内容(除了绑定(bind)其成员),您的代码就可以。 a 的存储空间在 C 的 c'tor 启动时已经分配,​​例如 Sneftel说,在范围内。因此,您可以引用它,如 [basic.life]/7明确允许:

Similarly, before the lifetime of an object has started but after the storage which the object will occupy has been allocated or, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, any glvalue that refers to the original object may be used but only in limited ways. For an object under construction or destruction, see [class.cdtor]. Otherwise, such a glvalue refers to allocated storage ([basic.stc.dynamic.deallocation]), and using the properties of the glvalue that do not depend on its value is well-defined. The program has undefined behavior if:

  • the glvalue is used to access the object, or
  • the glvalue is used to call a non-static member function of the object, or
  • the glvalue is bound to a reference to a virtual base class ([dcl.init.ref]), or
  • the glvalue is used as the operand of a dynamic_­cast or as the operand of typeid.

关于您的编辑:

What surprises me even more is that I can add a call to "a.doSth();" inside B constructor and this will also work. Why? At this moment the A object should not exist!

未定义的行为是未定义的。我链接到的段落中的第二个项目符号几乎说明了这一点。编译器可能足够聪明地捕捉到它,但并非必须如此。

关于c++ - 为什么它打破了初始化列表的顺序规则时会起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48441356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com