c++ - 初始化列表中 const 引用成员的初始化

标签 c++ gcc reference initialization initializer-list

我正在玩一些无用的代码来理解成员引用的初始化,并遇到了这个:

struct A {};

struct B
{
    B() : a()
    {
    }

    const A& a;
};

上面的代码在用 gcc 4.9.2 编译时出现以下错误:

In constructor 'B::B()':
error: value-initialization of reference type 'const A&'
  B() : a()

我明白了。

但是如果我在 B 的构造函数的初始化列表中使用统一初始化,就像这样:

struct A {};

struct B
{
    B() : a{}
    {
    }

    const A& a;
};

它编译得很好。

那么问题来了,为什么这里使用统一初始化会改变编译结果呢?

我还在 Microsoft Visual C++ 2013 中进行了尝试。 它不会编译任何一个版本的代码,并显示相同的错误消息:

Error 3 error C2440: 'initializing' : cannot convert from 'int' to 'const A & 

您可以在这里快速体验一下:

http://ideone.com/7f2t8I

最佳答案

GCC 对 {} 的解释是正确的。 [dcl.init.list]/p3.8-9(引用 N4296;较早的草案对这两个项目符号具有相同的相对顺序):

List-initialization of an object or reference of type T is defined as follows:

  • [7 inapplicable bullets omitted]

  • Otherwise, if T is a reference type, a prvalue temporary of the type referenced by T is copy-list-initialized or direct-list-initialized, depending on the kind of initialization for the reference, and the reference is bound to that temporary. [ Note: As usual, the binding will fail and the program is ill-formed if the reference type is an lvalue reference to a non-const type. —end note ]

  • Otherwise, if the initializer list has no elements, the object is value-initialized.

列表初始化引用命中项目符号 3.8,导致临时构造。 3.9 中的值初始化情况不适用。

引用的值初始化格式错误([dcl.init]/p9):

A program that calls for default-initialization or value-initialization of an entity of reference type is ill-formed.


但是,从 N4296 开始,根据 [class.base.init]/p8:

A temporary expression bound to a reference member in a mem-initializer is ill-formed.

这是由于 CWG issue 1696 而添加的,这是针对 C++14 的 DR(缺陷报告)。

CWG1696 之前的标准规定 (N4140 [class.temporary]/p5.1):

A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the constructor exits.

这意味着引用将在构建后立即变为悬挂。这可能促使 CWG1696 决定完全禁止此类绑定(bind)。

关于c++ - 初始化列表中 const 引用成员的初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28717682/

相关文章:

c++ - 将 char 指针值等同于某个 char 时出现段错误

c - 访问 C 文件中的汇编宏函数/指令

c - 为什么 GCC 在将有符号文字分配给无符号类型时不产生警告?

gcc - 我可以让 CMake 使用 gcc 增量链接生成 Makefile 吗?

c++ - 如何通过对构造函数的引用传递对象

c++ - 对静态函数的 undefined reference

c++ - 可执行文件中定义的函数名称(使用 dladdr)

c++ - 如何在 VS2010 中将 lambda 降级为函数指针?

c++ - 引用不更改变量的值

c++ - 使用 *this 初始化引用