• <small id='Dz8jV'></small><noframes id='Dz8jV'>

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

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

      1. 返回临时对象并绑定到 const 引用

        Returning temporary object and binding to const reference(返回临时对象并绑定到 const 引用)

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

                1. <tfoot id='6mtB7'></tfoot>
                  本文介绍了返回临时对象并绑定到 const 引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!


                  const 引用是否会延长临时引用的寿命?

                  我的编译器不会抱怨将临时分配给 const 引用:

                  My compiler doesn't complain about assigning temporary to const reference:

                  string foo() {
                    return string("123");
                  int main() {
                    const string& val = foo();
                  ", val.c_str());
                    return 0;

                  为什么?我认为从 foo 返回的字符串是临时的,val 可以指向生命周期已经结束的对象.C++ 标准是否允许这样做并延长返回对象的生命周期?

                  Why? I thought that string returned from foo is temporary and val can point to object which lifetime has finished. Does C++ standard allow this and prolongs the lifetime of returned object?


                  这是一个 C++ 特性.该代码是有效的,并且完全符合它的功能.

                  This is a C++ feature. The code is valid and does exactly what it appears to do.

                  通常,临时对象仅持续到它出现的完整表达式的结尾.但是,C++ 特意指定将临时对象绑定到堆栈上对 const 的引用会将临时对象的生命周期延长至引用本身的生命周期,从而避免常见的悬空引用错误.在上面的例子中, foo() 返回的临时值一直存在到右花括号为止.

                  Normally, a temporary object lasts only until the end of the full expression in which it appears. However, C++ deliberately specifies that binding a temporary object to a reference to const on the stack lengthens the lifetime of the temporary to the lifetime of the reference itself, and thus avoids what would otherwise be a common dangling-reference error. In the example above, the temporary returned by foo() lives until the closing curly brace.


                  P.S: This only applies to stack-based references. It doesn’t work for references that are members of objects.

                  全文:GotW #88:AHerb Sutter 的最重要的常量"候选者.

                  这篇关于返回临时对象并绑定到 const 引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



                  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;通过引用传递参数时可以放置吗?)
                  Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                  What is a dangling reference?(什么是悬空引用?)
                  C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)

                    <tbody id='X6M42'></tbody>
                  1. <legend id='X6M42'><style id='X6M42'><dir id='X6M42'><q id='X6M42'></q></dir></style></legend>
                    <tfoot id='X6M42'></tfoot>
                      • <bdo id='X6M42'></bdo><ul id='X6M42'></ul>

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

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