Is quot;rebindingquot; references in C++ like this legal?(是“重新绑定吗?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++ 中的引用像这样合法吗?
基础教程推荐
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01