C ++ Standard: End of Life

In the basic.life section of the C ++ standard, you can find the following (highlighting):

The lifetime of an object o of type T ends when:

  • if T is a class type with a non-trivial destructor ([class.dtor]), a call to the destructor begins, or

  • the storage that an object occupies is freed or reused by an object that is not nested in o ([intro.object]).

I am trying to find examples of storing an object o, reused by an object that is nested in o (contrary to what the standard says).

First I need to make sure that I understand what the standard means, "the storage that the object occupies [...] is reused by the object nested in o". First, to reuse storage, you must create a new object. Secondly, for o storage to be reused, a new object must be created in the memory location used by o. And finally, a new object must be created in a memory cell that will make the new object "nested in o", for example, in the location of an existing object that is "located inside o". Is it correct?

I thought of some examples, for example:

  • member of the association:

    union U { double d; int n; }; U u = {1.0}; new (&u.n) int;
    
  • An object created inside an array of characters:

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

Are they right? Are there any other examples?

Thank.

+4
source share
1

, " " . -, -

struct X {
    Y y;
};

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

x.

(http://eel.is/c++draft/intro.object#4) - , . unsigned char ( std::byte) , new. , , char .

, .

+5

Source: https://habr.com/ru/post/1693436/


All Articles