为什么可以将非常量引用参数绑定到临时对象?

Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)

本文介绍了为什么可以将非常量引用参数绑定到临时对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

char f1();void f2(char&);结构体{};一个 f3();void f4(A&);int main(){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++ 中,非常量引用可能是绑定到临时对象.现在,只能绑定临时对象到常量引用.

所以我认为这是VC++的一个bug.我已向 VC++ 团队

解决方案

如果你用 /Za 选项 禁用语言扩展,编译器拒绝这两个调用:

>cl/Za test.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 标头不能与/Za 选项一起#include.)

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.)

这篇关于为什么可以将非常量引用参数绑定到临时对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为什么可以将非常量引用参数绑定到临时对象?

基础教程推荐