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

标签 c++

为什么这段代码有效?我预计这会因为违反基本 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/

相关文章:

c++ - 无法使用opencv加载大图像

c++ - 当 -std=c++11 选项被删除时,clang++ 仅使用 boost::format 编译 C++11 程序

c++ - 读取 char * 始终具有相同的地址

c++ - 如何在 ODBC 中可移植地实现任意结果集的按行绑定(bind),同时避免对齐问题?

c++ - Makefile 中的条件表达式

c++ - C 字符串的长度:std::strlen() 与 std::char_traits<char>::length()

c++ - 尝试将参数传递给方法时出现“call to implicitly-deleted copy constructor of”错误

c++ - 错误 : 'string' in namespace 'std' does not name a type

c++ - 如何调试从 C++ DllImport 调用的 C++ dll

c++ - 如何重载运算符=