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

  2. <tfoot id='0IeeB'></tfoot>

        <bdo id='0IeeB'></bdo><ul id='0IeeB'></ul>

      <small id='0IeeB'></small><noframes id='0IeeB'>

      无法将临时对象作为参考传递

      时间:2023-10-07
      • <bdo id='nvhA0'></bdo><ul id='nvhA0'></ul>

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

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

              <tbody id='nvhA0'></tbody>

              <tfoot id='nvhA0'></tfoot><legend id='nvhA0'><style id='nvhA0'><dir id='nvhA0'><q id='nvhA0'></q></dir></style></legend>

                本文介绍了无法将临时对象作为参考传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                这是一个非常小的例子:

                This is a very minimal example:

                class Foo
                {
                public:
                    Foo(int x) {};
                };
                
                void ProcessFoo(Foo& foo)
                {
                }
                
                int main()
                {
                    ProcessFoo(Foo(42));
                    return 0;
                }
                

                以上在 Visual Studio 上编译良好,但在 Linux 和 Mac 上生成错误.

                The above compiles fine on Visual Studio, but generates an error on Linux and Mac.

                编译以上生成:

                $ g++ -std=c++11 -c newfile.cpp
                
                newfile.cpp: In function ‘int main()’:
                newfile.cpp:23:23: error: invalid initialization of non-const reference of type ‘Foo&’ from an rvalue of type ‘Foo’
                     ProcessFoo(Foo(42));
                                       ^
                newfile.cpp:14:6: note: in passing argument 1 of ‘void ProcessFoo(Foo&)’
                 void ProcessFoo(Foo& foo)
                

                我找到了三种解决方法:

                I've found three workarounds:

                1. 避免使用内联临时变量来调用 ProcessFoo.

                像这样:

                Foo foo42(42);
                ProcessFoo(foo42);
                

                1. ProcessFoo 采用常量引用:void ProcessFoo(const Foo& foo)

                ProcessFoo 只是让 Foo 按值传递.void ProcessFoo(Foo foo)

                ProcessFoo just lets Foo get passed by value. void ProcessFoo(Foo foo)

                为什么编译器禁止我的原始代码?(它在防范什么)?满足编译器的上述三种解决方法中的每一种是什么?MSVC 允许它做什么,但不允许使用 g++?

                Why is the compiler forbidding my original code? (What is it guarding against)? What is it about each of the three workarounds above that satisfies the compiler? What would MSVC allow it, but not g++?

                推荐答案

                按照设计,C++ 只允许将临时值传递给 const 引用、值或右值引用.这个想法是,采用非常量引用参数的函数声明它想要修改参数并允许它返回给调用者.使用临时文件这样做是没有意义的,而且很可能是一个错误.

                By design, C++ only allows a temporary to be passed to a const reference, value, or rvalue reference. The idea is that a function taking a non-const reference parameter is stating that it wants to modify the parameter and allowing it to go back to the caller. Doing so with a temporary is meaningless and most likely an error.

                而且我不知道您运行的是哪个版本的 g++.它在这里不起作用:http://coliru.stacked-crooked.com/a/43096cb398cbc973

                And I don't know what version of g++ you're running. It doesn't work here: http://coliru.stacked-crooked.com/a/43096cb398cbc973

                这篇关于无法将临时对象作为参考传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:MSVC++ 可变参数宏扩展 下一篇:什么是非常简单的 C++ 分析器 (VC++)?

                相关文章

                最新文章

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

                  <legend id='3JE9C'><style id='3JE9C'><dir id='3JE9C'><q id='3JE9C'></q></dir></style></legend>
                  <tfoot id='3JE9C'></tfoot>
                  1. <small id='3JE9C'></small><noframes id='3JE9C'>