为什么非 const 引用参数可以绑定到临时对象?
char f1();无效 f2(char&);结构 A {};一个 f3();无效 f4(A&);主函数(){f2(f1());//错误 C2664.这正如预期的那样.f4(f3());//行!为什么???}
<块引用>
错误 C2664: 'void f4(char &)' : 无法从 'char' 转换参数 1到 'char &'
有人告诉我,在 C++ 中,非常量引用参数不能绑定到临时对象;而在上面的代码中,f2(f1());
会按预期触发错误.
但是,为什么同样的规则不适用于代码行f4(f3());
?
PS:我的编译器是VC++ 2013.即使我注释了f2(f1());
这一行,那么包含f4(f3());
的代码将被编译,没有任何错误或警告.
更新:
MSDN 说:
<块引用>在以前的 Visual C++ 版本中,非常量引用可以是绑定到临时对象.现在,临时对象只能绑定到 const 引用.
所以我认为这是 VC++ 的一个错误.我已向 VC++ 团队
解决方案如果你用 /Za 选项 禁用语言扩展,编译器拒绝两个调用:
<代码>>cl/Za 测试.cppMicrosoft (R) C/C++ 优化编译器版本 18.00.21005.1 for x86版权所有 (C) 微软公司.版权所有.测试.cpptest.cpp(11): 错误 C2664: 'void f2(char &)' : 无法将参数 1 从 'char' 转换为 'char &'test.cpp(12): 错误 C2664: 'void f4(A &)' : 无法将参数 1 从 'A' 转换为 'A &'非常量引用只能绑定到左值
在几种(非常受限的)情况下,启用语言扩展的编译器仍然允许非常量左值引用绑定到右值表达式.我的理解是,这主要是为了避免破坏依赖此扩展"的几个庞大的遗留代码库.
(一般来说,不推荐使用/Za 的原因有很多,但主要是因为 Windows SDK 标头不能#include 与/Za 选项.)
char f1();
void f2(char&);
struct A {};
A f3();
void f4(A&);
int main()
{
f2(f1()); // error C2664. This is as expected.
f4(f3()); // OK! Why???
}
error C2664: 'void f4(char &)' : cannot convert argument 1 from 'char' to 'char &'
I have been taught that in C++ a non-const reference parameter cannot be bound to a temporary object; and in the code above, f2(f1());
triggers an error as expected.
However, why does the same rule not apply to the code line f4(f3());
?
PS: My compiler is VC++ 2013. Even if I comment the line f2(f1());
, then the code containing f4(f3());
will be compiled without any errors or warnings.
Update:
MSDN says:
In previous releases of Visual C++, non-const references could be bound to temporary objects. Now, temporary objects can only be bound to const references.
So I think it is a bug of VC++. I have submitted a bug report to VC++ team
解决方案If you compile with the /Za option to disable language extensions, the compiler rejects both calls:
> cl /Za test.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
test.cpp
test.cpp(11): error C2664: 'void f2(char &)' : cannot convert argument 1 from 'char' to 'char &'
test.cpp(12): error C2664: 'void f4(A &)' : cannot convert argument 1 from 'A' to 'A &'
A non-const reference may only be bound to an lvalue
There are several (very constrained) circumstances in which the compiler, with language extensions enabled, will still allow a non-const lvalue reference to bind to an rvalue expression. My understanding is that this is largely to avoid breaking several enormous legacy codebases that rely on this "extension."
(In general, use of /Za is not recommended for many reasons, but mostly because the Windows SDK headers cannot be #included with the /Za option.)
相关文章