Difference between revisions of "cpp/memory/start lifetime as"
From cppreference.com
D41D8CD98F (Talk | contribs) m (→Example) |
m (→Example: deterministic now.) |
||
Line 73: | Line 73: | ||
{{example | {{example | ||
|code= | |code= | ||
+ | #include <bit> | ||
+ | #include <cstdint> | ||
#include <iostream> | #include <iostream> | ||
#include <memory> | #include <memory> | ||
− | |||
int main() | int main() | ||
{ | { | ||
− | + | static_assert(std::endian::native == std::endian::little); | |
+ | std::uint64_t val = 0x5f4f4c4c45485f; | ||
// auto p = reinterpret_cast<char*>(&val); | // auto p = reinterpret_cast<char*>(&val); | ||
Line 87: | Line 89: | ||
std::cout.write(p, sizeof val); // OK | std::cout.write(p, sizeof val); // OK | ||
} | } | ||
− | |||
|output= | |output= | ||
_HELLO_ | _HELLO_ |
Revision as of 14:56, 24 June 2023
Defined in header <memory>
|
||
start_lifetime_as |
||
template< class T > T* start_lifetime_as( void* p ) noexcept; |
(1) | (since C++23) |
template< class T > const T* start_lifetime_as( const void* p ) noexcept; |
(2) | (since C++23) |
template< class T > volatile T* start_lifetime_as( volatile void* p ) noexcept; |
(3) | (since C++23) |
template< class T > const volatile T* start_lifetime_as( const volatile void* p ) noexcept; |
(4) | (since C++23) |
start_lifetime_as_array |
||
template< class T > T* start_lifetime_as_array( void* p, std::size_t n ) noexcept; |
(5) | (since C++23) |
template< class T > const T* start_lifetime_as_array( const void* p, |
(6) | (since C++23) |
template< class T > volatile T* start_lifetime_as_array( volatile void* p, |
(7) | (since C++23) |
template< class T > const volatile T* start_lifetime_as_array( const volatile void* p, |
(8) | (since C++23) |
1-4) Implicitly creates a complete object of type
T
(whose address is p) and objects nested within it. The value of each created object obj
of TriviallyCopyable type U
is determined in the same manner as for a call to std::bit_cast<U>(E) except that the storage is not actually accessed, where E
is the lvalue of type U
denoting obj
. Otherwise, the values of such created objects are unspecified.
-
T
shall be an ImplicitLifetimeType and shall be a complete type, otherwise the program is ill-formed. - The behavior is undefined if:
-
[
p,
(char*)p + sizeof(T))
does not denote a region of allocated storage that is a subset of the region of storage reachable through p, or - the region is not suitably aligned for the
T
.
-
- Note that the unspecified value can be indeterminate.
5-8) Implicitly creates an object array of type
T
with length n. To be precise, if n > 0 is true, it is equivalent to std::start_lifetime_as<U>(p) where U
is the type "array of n T
s". Otherwise, the function has no effects.
-
T
shall be a complete type, otherwise the program is ill-formed. - The behavior is undefined if:
- Non-null
p
is not suitably aligned for an array ofT
, or - n <= std::size_t(-1) / sizeof(T) is false, or
- n > 0 and
[
(char*)p,
(char*)p + (n * sizeof(T)))
does not denote a region of allocated storage that is a subset of the region of storage reachable through p.
- Non-null
Contents |
Parameters
p | - | possible cv-qualified void pointer, denotes the address of the region consisting objects |
n | - | std::size_t, denotes the size of the object array to be created |
Return value
1-4) A pointer to the complete object as described above.
5-8) A pointer to the first element of the created array, if any; otherwise, a pointer that compares equal to p.
Notes
new (void_ptr) std::byte[size] works as an untyped version of this operation.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_start_lifetime_as |
202207L | (C++23) | Explicit lifetime management |
Example
Run this code
#include <bit> #include <cstdint> #include <iostream> #include <memory> int main() { static_assert(std::endian::native == std::endian::little); std::uint64_t val = 0x5f4f4c4c45485f; // auto p = reinterpret_cast<char*>(&val); // std::cout.write(p, sizeof val); // UB: p does not point to a char array auto p = std::start_lifetime_as_array<char>(&val, sizeof val); std::cout.write(p, sizeof val); // OK }
Output:
_HELLO_
References
- C++23 standard (ISO/IEC 14882:2024):
- 20.2.6 Explicit lifetime management [obj.lifetime]
See also
(C++20) |
reinterpret the object representation of one type as that of another (function template) |
(C++17) |
pointer optimization barrier (function template) |