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

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

        <legend id='KnrUG'><style id='KnrUG'><dir id='KnrUG'><q id='KnrUG'></q></dir></style></legend>
      2. <tfoot id='KnrUG'></tfoot>

        是“重新绑定"吗?C++ 中的引用像这样合法吗?

        Is quot;rebindingquot; references in C++ like this legal?(是“重新绑定吗?C++ 中的引用像这样合法吗?)

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

              <bdo id='sP1mp'></bdo><ul id='sP1mp'></ul>
                <legend id='sP1mp'><style id='sP1mp'><dir id='sP1mp'><q id='sP1mp'></q></dir></style></legend>
                <i id='sP1mp'><tr id='sP1mp'><dt id='sP1mp'><q id='sP1mp'><span id='sP1mp'><b id='sP1mp'><form id='sP1mp'><ins id='sP1mp'></ins><ul id='sP1mp'></ul><sub id='sP1mp'></sub></form><legend id='sP1mp'></legend><bdo id='sP1mp'><pre id='sP1mp'><center id='sP1mp'></center></pre></bdo></b><th id='sP1mp'></th></span></q></dt></tr></i><div id='sP1mp'><tfoot id='sP1mp'></tfoot><dl id='sP1mp'><fieldset id='sP1mp'></fieldset></dl></div>
                  <tbody id='sP1mp'></tbody>
                • <tfoot id='sP1mp'></tfoot>
                  本文介绍了是“重新绑定"吗?C++ 中的引用像这样合法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  以下在 C++ 中合法吗?

                  Is the following legal in C++?

                  据我所知,Reference 有一个微不足道的析构函数,所以它应该是合法的.
                  但我认为引用不能合法反弹......他们可以吗?

                  As far as I can tell, Reference has a trivial destructor, so it should be legal.
                  But I thought references can't be rebound legally... can they?

                  template<class T>
                  struct Reference
                  {
                      T &r;
                      Reference(T &r) : r(r) { }
                  };
                  
                  int main()
                  {
                      int x = 5, y = 6;
                      Reference<int> r(x);
                      new (&r) Reference<int>(y);
                  }
                  

                  推荐答案

                  我想我在下面的一段话中找到了答案,引用"了一段谈论琐碎的 dtor/dtor 副作用,即[基本生活]/7:

                  I think I found the answer in a passage below the "quoted" one that talks about trivial dtor / dtor side effects, namely [basic.life]/7:

                  如果在一个对象的生命周期结束后,在该对象占用的存储空间被重用或释放之前,在原对象占用的存储位置创建一个新对象,一个指向原对象的指针,引用原始对象的引用或原始对象的名称将自动引用新对象,并且一旦新对象的生命周期开始,就可以用于操作新对象,如果:

                  If, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, a new object is created at the storage location which the original object occupied, a pointer that pointed to the original object, a reference that referred to the original object, or the name of the original object will automatically refer to the new object and, once the lifetime of the new object has started, can be used to manipulate the new object, if:

                  • 新对象的存储与原对象占用的存储位置完全重叠,并且

                  • the storage for the new object exactly overlays the storage location which the original object occupied, and

                  新对象与原始对象的类型相同(忽略顶级 cv 限定符),并且

                  the new object is of the same type as the original object (ignoring the top-level cv-qualifiers), and

                  原始对象的类型不是const限定的,并且,如果是类类型,不包含任何类型为const限定或引用类型的非静态数据成员,并且

                  the type of the original object is not const-qualified, and, if a class type, does not contain any non-static data member whose type is const-qualified or a reference type, and

                  原始对象是 T 类型的最派生对象,新对象是 T 类型的最派生对象(也就是说,它们不是基类子对象).

                  the original object was a most derived object of type T and the new object is a most derived object of type T (that is, they are not base class subobjects).

                  通过重用存储,我们结束了原始对象 [basic.life]/1 的生命周期

                  By reusing the storage, we end the lifetime of original object [basic.life]/1

                  T 类型的对象的生命周期在以下情况下结束:

                  The lifetime of an object of type T ends when:

                  • 如果 T 是具有非平凡析构函数的类类型,则析构函数调用开始,或

                  • if T is a class type with a non-trivial destructor, the destructor call starts, or

                  对象占用的存储空间被重用或释放.

                  the storage which the object occupies is reused or released.

                  所以我认为 [basic.life]/7 涵盖了这种情况

                  So I think [basic.life]/7 covers the situation

                  Reference<int> r(x);
                  new (&r) Reference<int>(y);
                  

                  我们在这里结束由 r 表示的对象的生命周期,并在同一位置创建一个新对象.

                  where we end the lifetime of the object denoted by r, and create a new object at the same location.

                  由于 Reference 是具有引用数据成员的类类型,因此满足 [basic.life]/7 的要求.也就是说,r 甚至可能不引用新对象,我们可能不会使用它来操纵"这个新创建的对象(我将这种操纵"也解释为只读访问).

                  As Reference<int> is a class type with a reference data member, the requirements of [basic.life]/7 are not fulfilled. That is, r might not even refer to the new object, and we may not use it to "manipulate" this newly created object (I interpret this "manipulate" also as read-only accesses).

                  这篇关于是“重新绑定"吗?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++ 引用发生变化)

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

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

                      <tbody id='P4Gc1'></tbody>

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

                          <tfoot id='P4Gc1'></tfoot>