复制空对象是否涉及访问它 [英] Does copying an empty object involve accessing it

查看:77
本文介绍了复制空对象是否涉及访问它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

灵感来自问题.

struct E {};
E e;
E f(e);  // Accesses e?

访问

读取或修改对象的值

read or modify the value of an object

空类具有隐式定义的副本构造函数

非工会类X的隐式定义的复制/移动构造函数执行其基和成员的成员式复制/移动. [...]初始化的顺序与用户定义的构造函数中的基和成员的初始化的顺序相同.假设x是构造函数的参数,或者对于move构造函数,是引用该参数的xvalue.每个基本或非静态数据成员都按照适合其类型的方式进行复制/移动:

The implicitly-defined copy/move constructor for a non-union class X performs a memberwise copy/move of its bases and members. [...] The order of initialization is the same as the order of initialization of bases and members in a user-defined constructor. Let x be either the parameter of the constructor or, for the move constructor, an xvalue referring to the parameter. Each base or non-static data member is copied/moved in the manner appropriate to its type:

  • [...]基础或成员直接用x的相应基础或成员初始化.
  • [...] the base or member is direct-initialized with the corresponding base or member of x.

推荐答案

我认为标准中最精确地描述执行访问权限的部分是[basic.life].在本段中,说明了使用引用或指向超出其生命周期的对象的指针可以完成的操作.由于此类值不存在(否则该标准会不一致),因此有权与此类实体进行任何操作的对象均无法访问该对象的值.

I think that the part of the standard that describes the most precisely what performs an access is [basic.life]. In this paragraph it is explained what can be done with a reference that refers to, or a pointer that point to, an object which is out of its lifetime period. Everything that is authorized to do with such entities does not perform an access to the object value since such value does not exist (otherwise the standard would be inconsistent).

因此,如果这不是未定义的行为,那么我们可以举一个更激烈的示例,因此您的示例代码中没有访问e的权限(根据上述推理):

So we can take a more drastic example, if this is not undefined behavior, so there are no access to e in your example code (accordingly to the reasonning above):

struct E{
     E()=default;
     E(const E&){}
     };
E e;
e.~E();
E f(e);

此处e是一个对象,该对象的生命周期已经结束,但是其存储仍在分配中. [basic.life]中介绍了使用这种左值可以做什么/6

Here e is an object whose lifetime has ended but whose storage is still allocated. What can be done with such a lvalue is described in [basic.life]/6

类似地,在对象的生存期开始之前但已分配了该对象将占用的存储空间之后,或者在对象的生存期结束之后以及在重新使用或释放​​该对象占用的存储空间之前,所有glvalue可以使用指代原始对象的符号,但只能以有限的方式使用.有关正在构造或销毁的对象,请参见[class.cdtor].否则,此类glvalue会引用已分配的存储([basic.stc.dynamic.deallocation]),并且使用不依赖于glvalue的值的属性是明确定义的.该程序在以下情况下具有未定义的行为:

Similarly, before the lifetime of an object has started but after the storage which the object will occupy has been allocated or, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, any glvalue that refers to the original object may be used but only in limited ways. For an object under construction or destruction, see [class.cdtor]. Otherwise, such a glvalue refers to allocated storage ([basic.stc.dynamic.deallocation]), and using the properties of the glvalue that do not depend on its value is well-defined. The program has undefined behavior if:

  • 将左值到右值转换([conv.lval])应用于这样的glvalue,

  • an lvalue-to-rvalue conversion ([conv.lval]) is applied to such a glvalue,

glvalue用于访问对象的非静态数据成员或调用对象的非静态成员函数,或者

the glvalue is used to access a non-static data member or call a non-static member function of the object, or

glvalue隐式转换([conv.ptr])为对基类类型的引用,或者

the glvalue is implicitly converted ([conv.ptr]) to a reference to a base class type, or

glvalue用作static_cast([expr.static.cast])的操作数,除非最终转换为cv char&或cv unsigned char&或

the glvalue is used as the operand of a static_cast ([expr.static.cast]) except when the conversion is ultimately to cv char& or cv unsigned char&, or

glvalue用作dynamic_cast([expr.dynamic.cast])的操作数或typeid的操作数.

the glvalue is used as the operand of a dynamic_cast ([expr.dynamic.cast]) or as the operand of typeid.

上面的引用点都没有在E复制构造函数中发生,因此此答案中的示例代码已得到很好的定义,这意味着无法访问已破坏对象的值.因此,您的示例代码中无法访问e.

None of the cited point above does happen inside E copy constructor so the example code in this answer is well defined, which implies that there have been no access to the value of the destroyed object. So there is no access to e in your example code.

这篇关于复制空对象是否涉及访问它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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