Why can#39;t I initialize a reference in an initializer list with uniform initialization?(为什么我不能使用统一初始化初始化初始化列表中的引用?)
问题描述
也就是说,为什么会这样:
That is, why does this:
struct S {};
struct T
{
T(S& s) : s{s} {}
S& s;
};
int main()
{
S s;
T t{s};
}
给我一个 GCC 4.7 的编译器错误:
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>'
?
要修复错误,我必须将 s{s} 更改为 s(s).这不会破坏,呃,统一初始化的统一性吗?
To fix the error, I have to change the s{s} to s(s). Doesn't this break the, erm, uniformity of uniform initialization?
编辑:我尝试使用 clang,并且 clang 接受了它,所以也许这是一个 GCC 错误?
EDIT: I tried with clang, and clang accepts it, so perhaps it's a GCC bug?
推荐答案
是的,它是一个 错误.这是新事物,并在 2012 年 2 月的工作文件中投票(link).
Yes, its a bug. This is something new and was voted in the working paper in February 2012 (link).
Nicol Bolas 说得很好,因为 gcc 实际上是符合 FDIS 批准的 C++11 标准的编译器,因为在此之后对工作文件进行了更改.
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.
这篇关于为什么我不能使用统一初始化初始化初始化列表中的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么我不能使用统一初始化初始化初始化列表中的引用?
基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- CString 到 char* 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
