为什么无法使用统一初始化初始化初始化列表中的引用? [英] Why can't I initialize a reference in an initializer list with uniform initialization?

查看:330
本文介绍了为什么无法使用统一初始化初始化初始化列表中的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是为什么:

  struct S {}; 

struct T
{
T(S& s):s {s} {}

S& s;
};

int main()
{
S s;
T t {s};
}

给我一​​个GCC 4.7的编译器错误:

  test.cpp:在构造函数'T :: T(S&)':
test.cpp:5:18:


$ b的类型的右值从<大括号括起始化列表> $ b



要修正错误,我必须将 s {s} 更改为 s(s)

编辑:我尝试了clang, clang接受它,所以也许是一个GCC错误?

解决方案

是的,它的a bug 。这是一项新功能,并于2012年2月在工作文件中投票( link < a>)。



提出了一个好处,因为gcc实际上是根据FDIS批准的C ++ 11标准的符合标准的编译器,因为对工作纸是在那之后。


That is, why does this:

struct S {};

struct T
{
    T(S& s) : s{s} {}

    S& s;
};

int main()
{
    S s;
    T t{s};
}

give me a compiler error with GCC 4.7:

test.cpp: In constructor 'T::T(S&)':
test.cpp:5:18: error: invalid initialization of non-const reference of type 'S&' from an rvalue of type '<brace-enclosed initializer list>'

?

To fix the error, I have to change the s{s} to s(s). Doesn't this break the, erm, uniformity of uniform initialization?

EDIT: I tried with clang, and clang accepts it, so perhaps it's a GCC bug?

解决方案

Yes, its a bug. This is something new and was voted in the working paper in February 2012 (link).

Nicol Bolas makes a good point in that gcc is actually the conforming compiler according to the FDIS approved C++11 standard because the changes to the working paper were made after that.

这篇关于为什么无法使用统一初始化初始化初始化列表中的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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