简体   繁体   English

C ++标准:生命周期结束

[英]C++ Standard: end of lifetime

In the basic.life section of the C++ standard, one can find the following (emphasis mine): 在C ++标准的basic.life部分,可以找到以下内容(强调我的):

The lifetime of an object o of type T ends when: 类型T的对象o的生命周期结束时:

  • if T is a class type with a non-trivial destructor ([class.dtor]), the destructor call starts, or 如果T是具有非平凡析构函数([class.dtor])的类类型,则析构函数调用将启动,或者

  • the storage which the object occupies is released, or is reused by an object that is not nested within o ([intro.object]). 对象占用的存储被释放,或被未嵌套在o中对象重用([intro.object])。

I am trying to find examples of the storage of the object o being reused by an object that is nested within o (the opposite of what the standard says). 我试图找到对象的存储示例o嵌套在o中的对象重用(与标准所说的相反)。

First I need to make sure I understand what the standard means by "the storage which the object occupies [...] is reused by an object that is nested within o". 首先,我需要确保我理解标准的含义是“对象占用的存储被嵌套在o中的对象重用”。 First, in order for storage to be reused, a new object has to be created. 首先,为了重用存储,必须创建一个新对象。 Second, for o 's storage to be reused, the new object has to be created in the memory location used by o . 其次,为了重用o的存储,必须在o使用的内存位置创建新对象。 And finally, the new object has to be created in a memory location that would make the new object "nested within o", for example in the location of an already existing object that is "nesten within o". 最后,必须在一个内存位置创建新对象,该位置将使新对象“嵌套在o中”,例如在已经存在的对象“nesten in o”的位置。 Is this correct? 它是否正确?

I thought of some examples, such as: 我想到了一些例子,例如:

  • union member: 工会会员:

     union U { double d; int n; }; U u = {1.0}; new (&u.n) int; 
  • object created inside array of chars: 在chars数组中创建的对象:

     char mem[sizeof(int)]; new (mem) int; 

Are these correct? 这些是正确的吗? Are there other examples? 还有其他例子吗?

Thank you. 谢谢。

I believe, by "nested objects here" the standard is simply referring to member subobjects here. 我相信,通过“嵌套对象在这里”标准只是指这里的成员子对象。 Since formally, member subobjects occupy the storage of their container object, without this exception something like this code 从形式上看,成员子对象占用其容器对象的存储空间,没有像此代码那样的异常

struct X {
    Y y;
};

void foo(X& x) {
    new (x.y) Y;
}

Would end the storage of x . 将结束x的存储。

Another example of nested objects per Standard ( http://eel.is/c++draft/intro.object#4 ) is a case when one object provides storage for another. 每个标准( http://eel.is/c++draft/intro.object#4 )的嵌套对象的另一个示例是一个对象为另一个对象提供存储的情况。 An unsigned char (or std::byte ) array can provide storage for another object, if it is created inside it with placement new. unsigned char (或std::byte )数组可以为另一个对象提供存储,如果它是在其中创建的,则为placement new。 This is almost your second example, with the exception that in your case the char could be signed or unsigned. 这几乎是您的第二个示例,但在您的情况下,char可以是签名或未签名的。

Your example of unions is correct example of a non-nested object occupying the same storage. 您的联合示例是占用相同存储的非嵌套对象的正确示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM