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

    <tfoot id='JplnP'></tfoot>

      1. <small id='JplnP'></small><noframes id='JplnP'>

        <legend id='JplnP'><style id='JplnP'><dir id='JplnP'><q id='JplnP'></q></dir></style></legend>
      2. 无法将临时对象作为参考传递

        Can#39;t pass temporary object as reference(无法将临时对象作为参考传递)
        <i id='JvaWF'><tr id='JvaWF'><dt id='JvaWF'><q id='JvaWF'><span id='JvaWF'><b id='JvaWF'><form id='JvaWF'><ins id='JvaWF'></ins><ul id='JvaWF'></ul><sub id='JvaWF'></sub></form><legend id='JvaWF'></legend><bdo id='JvaWF'><pre id='JvaWF'><center id='JvaWF'></center></pre></bdo></b><th id='JvaWF'></th></span></q></dt></tr></i><div id='JvaWF'><tfoot id='JvaWF'></tfoot><dl id='JvaWF'><fieldset id='JvaWF'></fieldset></dl></div>

        <tfoot id='JvaWF'></tfoot>
      3. <legend id='JvaWF'><style id='JvaWF'><dir id='JvaWF'><q id='JvaWF'></q></dir></style></legend>
        • <bdo id='JvaWF'></bdo><ul id='JvaWF'></ul>
          1. <small id='JvaWF'></small><noframes id='JvaWF'>

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

                  问题描述

                  这是一个非常小的例子:

                  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

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

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

                  相关文档推荐

                  Consistent pseudo-random numbers across platforms(跨平台一致的伪随机数)
                  Vary range of uniform_int_distribution(改变uniform_int_distribution的范围)
                  What is a seed in terms of generating a random number?(就生成随机数而言,种子是什么?)
                  Is 1.0 a valid output from std::generate_canonical?(1.0 是 std::generate_canonical 的有效输出吗?)
                  Getting big random numbers in C/C++(在 C/C++ 中获取大随机数)
                  What is the best way to generate random numbers in C++?(在 C++ 中生成随机数的最佳方法是什么?)

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

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

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