• <bdo id='zBUyn'></bdo><ul id='zBUyn'></ul>

    <tfoot id='zBUyn'></tfoot>
    1. <legend id='zBUyn'><style id='zBUyn'><dir id='zBUyn'><q id='zBUyn'></q></dir></style></legend>
      <i id='zBUyn'><tr id='zBUyn'><dt id='zBUyn'><q id='zBUyn'><span id='zBUyn'><b id='zBUyn'><form id='zBUyn'><ins id='zBUyn'></ins><ul id='zBUyn'></ul><sub id='zBUyn'></sub></form><legend id='zBUyn'></legend><bdo id='zBUyn'><pre id='zBUyn'><center id='zBUyn'></center></pre></bdo></b><th id='zBUyn'></th></span></q></dt></tr></i><div id='zBUyn'><tfoot id='zBUyn'></tfoot><dl id='zBUyn'><fieldset id='zBUyn'></fieldset></dl></div>
    2. <small id='zBUyn'></small><noframes id='zBUyn'>

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

        Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
        1. <tfoot id='tr0DB'></tfoot>

              <i id='tr0DB'><tr id='tr0DB'><dt id='tr0DB'><q id='tr0DB'><span id='tr0DB'><b id='tr0DB'><form id='tr0DB'><ins id='tr0DB'></ins><ul id='tr0DB'></ul><sub id='tr0DB'></sub></form><legend id='tr0DB'></legend><bdo id='tr0DB'><pre id='tr0DB'><center id='tr0DB'></center></pre></bdo></b><th id='tr0DB'></th></span></q></dt></tr></i><div id='tr0DB'><tfoot id='tr0DB'></tfoot><dl id='tr0DB'><fieldset id='tr0DB'></fieldset></dl></div>

                <bdo id='tr0DB'></bdo><ul id='tr0DB'></ul>

                <small id='tr0DB'></small><noframes id='tr0DB'>

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

                  问题描述

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

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                  C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                  Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)
                  Why does std::forward have two overloads?(为什么 std::forward 有两个重载?)
                    <tbody id='hQ8pG'></tbody>
                      <bdo id='hQ8pG'></bdo><ul id='hQ8pG'></ul>

                        <small id='hQ8pG'></small><noframes id='hQ8pG'>

                      1. <i id='hQ8pG'><tr id='hQ8pG'><dt id='hQ8pG'><q id='hQ8pG'><span id='hQ8pG'><b id='hQ8pG'><form id='hQ8pG'><ins id='hQ8pG'></ins><ul id='hQ8pG'></ul><sub id='hQ8pG'></sub></form><legend id='hQ8pG'></legend><bdo id='hQ8pG'><pre id='hQ8pG'><center id='hQ8pG'></center></pre></bdo></b><th id='hQ8pG'></th></span></q></dt></tr></i><div id='hQ8pG'><tfoot id='hQ8pG'></tfoot><dl id='hQ8pG'><fieldset id='hQ8pG'></fieldset></dl></div>
                          <tfoot id='hQ8pG'></tfoot>

                        1. <legend id='hQ8pG'><style id='hQ8pG'><dir id='hQ8pG'><q id='hQ8pG'></q></dir></style></legend>