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

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

        C++ 常量引用生存期(容器适配器)

        C++ constant reference lifetime (container adaptor)(C++ 常量引用生存期(容器适配器))

        <legend id='MllMN'><style id='MllMN'><dir id='MllMN'><q id='MllMN'></q></dir></style></legend>

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

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

                  <tfoot id='MllMN'></tfoot>
                1. 本文介绍了C++ 常量引用生存期(容器适配器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有这样的代码:

                  class T {};
                  
                  class container {
                   const T &first, T &second;
                   container(const T&first, const T & second);
                  };
                  
                  class adapter : T {};
                  
                  container(adapter(), adapter());
                  

                  我认为常量引用的生命周期就是容器的生命周期.但是,看起来相反,创建容器后适配器对象被销毁,留下悬空引用.

                  I thought lifetime of constant reference would be lifetime of container. However, it appears otherwise, adapter object is destroyed after container is created, leaving dangling reference.

                  什么是正确的生命周期?

                  What is the correct lifetime?

                  适配器临时对象的栈作用域是容器对象的作用域还是容器构造函数的作用域?

                  is stack scope of adapter temporary object the scope of container object or of container constructor?

                  如何正确实现绑定临时对象到类成员引用?

                  how to correctly implement binding temporary object to class member reference?

                  谢谢

                  推荐答案

                  根据 C++03 标准,对引用的临时绑定根据上下文具有不同的生命周期.在您的示例中,我认为下面突出显示的部分适用(12.2/5临时对象"):

                  According to the C++03 standard, a temporary bound to a reference has differing lifetimes depending on the context. In your example, I think the highlighted portion below applies (12.2/5 "Temporary objects"):

                  引用绑定的临时对象或作为临时对象绑定的子对象的完整对象的临时对象在引用的生命周期内持续存在,除非在下面指定.临时绑定到构造函数的 ctor-initializer (12.6.2) 中的引用成员会一直存在,直到构造函数退出.在函数调用 (5.2.2) 中临时绑定到引用参数会一直存在,直到包含调用的完整表达式完成.

                  The temporary to which the reference is bound or the temporary that is the complete object to a subobject of which the temporary is bound persists for the lifetime of the reference except as specified below. A temporary bound to a reference member in a constructor’s ctor-initializer (12.6.2) persists until the constructor exits. A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call.

                  因此,虽然绑定临时对象是延长临时对象生命周期的高级技术(GotW #88:最重要的常量"的候选者),在这种情况下它显然无济于事.

                  So while binding a temporary is an advanced technique to extend the lifetime of the temporary object (GotW #88: A Candidate For the "Most Important const"), it apparently won't help you in this case.

                  另一方面,Eric Niebler 有一篇您可能感兴趣的文章,该文章讨论了一种有趣的(如果令人费解)技术,该技术可以让您的类的构造函数推断是否已将临时对象(实际上是右值)传递给它(因此必须复制)或传递的非临时(左值)(因此可能安全地将引用隐藏而不是复制):

                  On the other hand, Eric Niebler has an article that you may be interested in that discusses an interesting (if convoluted) technique that could let your class's constructors deduce whether a temporary object (actually an rvalue) has been passed to it (and therefore would have to be copied) or a non-temporary (lvalue) as been passed (and therefore could potentially safely have a reference stashed away instead of copying):

                  • 有条件的爱:FOREACH Redux

                  祝你好运 - 每次我阅读这篇文章时,我都必须像以前从未见过的材料一样完成所有工作.它只在我身边停留片刻......

                  Good luck with it though - every time I read the article, I have to work through everything as if I've never seen the material before. It only sticks with me for a fleeting moment...

                  而且我应该提到 C++0x 的右值引用应该使 Niebler 的技术变得不必要.MSVC 2010 将支持 Rvalue 引用,该版本计划在一周左右发布(如果我没记错的话,将在 2010 年 4 月 12 日发布).我不知道右值引用在 GCC 中是什么状态.

                  And I should mention that C++0x's rvalue references should make Niebler's techniques unnecessary. Rvalue references will be supported by MSVC 2010 which is scheduled to be released in a week or so (on 12 April 2010 if I recall correctly). I don't know what the status of rvalue references is in GCC.

                  这篇关于C++ 常量引用生存期(容器适配器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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++ 引用发生变化)
                  <i id='k9ln7'><tr id='k9ln7'><dt id='k9ln7'><q id='k9ln7'><span id='k9ln7'><b id='k9ln7'><form id='k9ln7'><ins id='k9ln7'></ins><ul id='k9ln7'></ul><sub id='k9ln7'></sub></form><legend id='k9ln7'></legend><bdo id='k9ln7'><pre id='k9ln7'><center id='k9ln7'></center></pre></bdo></b><th id='k9ln7'></th></span></q></dt></tr></i><div id='k9ln7'><tfoot id='k9ln7'></tfoot><dl id='k9ln7'><fieldset id='k9ln7'></fieldset></dl></div>

                  • <bdo id='k9ln7'></bdo><ul id='k9ln7'></ul>
                    <tfoot id='k9ln7'></tfoot>

                    <legend id='k9ln7'><style id='k9ln7'><dir id='k9ln7'><q id='k9ln7'></q></dir></style></legend>
                      • <small id='k9ln7'></small><noframes id='k9ln7'>

                            <tbody id='k9ln7'></tbody>