At what point is a trivial type lifetime created by posting a new launch?

While immersed in dynamic memory, it seemed contradictory to me how trivial types begin their lives. Consider a fragment

void* p = ::operator new(sizeof(int));  // 1
// 2
new (p) int; // 3

When intdoes your life begin?

  • Only acquires memory, is ::operator newindicated to have an effect (from [new.delete.single] )

    Distribution functions called by the new expression to allocate bytes of memory size. [...] allocates storage properly aligned to represent any object of this size if the type of the object does not have alignment with the extension extension.

    Given that it is not enough to create an object , intit cannot begin to exist here.

  • At this point, storage was purchased for lampshades for int.

  • intcreated by placing new. But for some reason, his life here did not begin, since from [basic.life]

    [...] They say that an object has non-empty initialization if it belongs to a class or an aggregate, and it or one of its subobjects is initialized with a constructor other than the trivial default constructor. The lifetime of an object of type Tbegins when:

    • the storage with the correct alignment and size for the type is Treceived, and

    • if the object has a non-empty initialization, its initialization is completed [...]

    int , , . . , , , , .

. . a.allocate(n) a - T

n T, .

+6
1

, . new(p) int , [basic.life]/1, , new(p) int .

N4659 [expr.new] new(p) int ::operator new(sizeof(int), p). [new.delete.placement] ​​:

void* operator new(std::size_t size, void* ptr) noexcept;

: ptr.

: .

" " , , , , - , .

+7

All Articles