是“重新结合” C ++中的引用喜欢这个法律吗? [英] Is "rebinding" references in C++ like this legal?

查看:140
本文介绍了是“重新结合” C ++中的引用喜欢这个法律吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中是否合法?

Is the following legal in C++?

据我所知,引用 ,所以应该是合法的。

但我认为引用不能合法地反弹...他们可以吗?

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副作用,即[basic.life] / 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:


  • if T 是一个具有非平凡析构函数的类类型,析构函数调用开始,或

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

所以我认为[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.

作为参考< int> 类型与参考数据成员,[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 ++中的引用喜欢这个法律吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆