20 Memory management library [mem]

20.2 Memory [memory]

20.2.6 Explicit lifetime management [obj.lifetime]

template<class T> T* start_lifetime_as(void* p) noexcept; template<class T> const T* start_lifetime_as(const void* p) noexcept; template<class T> volatile T* start_lifetime_as(volatile void* p) noexcept; template<class T> const volatile T* start_lifetime_as(const volatile void* p) noexcept;
Mandates: T is an implicit-lifetime type ([basic.types.general]) and not an incomplete type ([basic.types.general]).
Preconditions: [p, (char*)p + sizeof(T)) denotes a region of allocated storage that is a subset of the region of storage reachable through ([basic.compound]) p and suitably aligned for the type T.
Effects: Implicitly creates objects ([intro.object]) within the denoted region consisting of an object a of type T whose address is p, and objects nested within a, as follows: The object representation of a is the contents of the storage prior to the call to start_lifetime_as.
The value of each created object o of trivially copyable type ([basic.types.general]) U is determined in the same manner as for a call to bit_cast<U>(E) ([bit.cast]), where E is an lvalue of type U denoting o, except that the storage is not accessed.
The value of any other created object is unspecified.
[Note 1: 
The unspecified value can be indeterminate.
β€” end note]
Returns: A pointer to the a defined in the Effects paragraph.
template<class T> T* start_lifetime_as_array(void* p, size_t n) noexcept; template<class T> const T* start_lifetime_as_array(const void* p, size_t n) noexcept; template<class T> volatile T* start_lifetime_as_array(volatile void* p, size_t n) noexcept; template<class T> const volatile T* start_lifetime_as_array(const volatile void* p, size_t n) noexcept;
Mandates: T is a complete type.
Preconditions: p is suitably aligned for an array of T or is null.
n <= size_t(-1) / sizeof(T) is true.
If n > 0 is true, [(char*)p, (char*)p + (n * sizeof(T))) denotes a region of allocated storage that is a subset of the region of storage reachable through ([basic.compound]) p.
Effects: If n > 0 is true, equivalent to start_lifetime_as<U>(p) where U is the type β€œarray of n T”.
Otherwise, there are no effects.
Returns: A pointer to the first element of the created array, if any; otherwise, a pointer that compares equal to p ([expr.eq]).
template<class T> T* trivially_relocate(T* first, T* last, T* result);
Mandates: is_trivially_relocatable_v<T> && !is_const_v<T> is true.
T is not an array of unknown bound.
Preconditions:
  • [first, last) is a valid range.
  • [result, result + (last - first)) denotes a region of storage that is a subset of the region reachable through result ([basic.compound]) and suitably aligned for the type T.
  • No element in the range [first, last) is a potentially-overlapping subobject.
Postconditions: No effect if result == first is true.
Otherwise, the range denoted by [result, result + (last - first)) contains objects (including subobjects) whose lifetime has begun and whose object representations are the original object representations of the corresponding objects in the source range [first, last) except for any parts of the object representations used by the implementation to represent type information ([intro.object]).
If any of the objects has union type, its active member is the same as that of the corresponding object in the source range.
If any of the aforementioned objects has a non-static data member of reference type, that reference refers to the same entity as does the corresponding reference in the source range.
The lifetimes of the original objects in the source range have ended.
Returns: result + (last - first).
Throws: Nothing.
Complexity: Linear in the length of the source range.
Remarks: The destination region of storage is considered reused ([basic.life]).
No constructors or destructors are invoked.
[Note 2: 
Overlapping ranges are supported.
β€” end note]
template<class T> constexpr T* relocate(T* first, T* last, T* result);
Mandates: is_nothrow_relocatable_v<T> && !is_const_v<T> is true.
T is not an array of unknown bound.
Preconditions:
  • [first, last) is a valid range.
  • [result, result + (last - first)) denotes a region of storage that is a subset of the region reachable through result ([basic.compound]) and suitably aligned for the type T.
  • No element in the range [first, last) is a potentially-overlapping subobject.
Effects:
  • If result == first is true, no effect;
  • otherwise, if not called during constant evaluation and is_trivially_relocatable_v<T> is true, then has effects equivalent to: trivially_relocate(first, last, result);
  • otherwise, for each integer i in [0, last - first),
    • if T is an array type, equivalent to: relocate(begin(first[i]), end(first[i]), *start_lifetime_as<T>(result + i));
    • otherwise, equivalent to: construct_at(result + i, std​::​move(first[i])); destroy_at(first + i);
Returns: result + (last - first).
Throws: Nothing.
[Note 3: 
Overlapping ranges are supported.
β€” end note]