std::start_...ime_as_array(3)
π NAME π
std::start_lifetime_as,std::start_lifetime_as_array β std::start_lifetime_as,std::start_lifetime_as_array
okay bestie, welcome to the std::start_...ime_as_array(3) page from the C++ Standard Libary π
this is the lifetime-management corner of the group chat, and we're talking about the std::start_lifetime_as and std::start_lifetime_as_array duo π«Ά
β¨ Synopsis β¨
Defined in header <memory> β yes, that's the include, bestie πΈ
start_lifetime_as
template< class T >
(1) (since C++23)
T* start_lifetime_as( void* p ) noexcept;
template< class T >
(2) (since C++23)
const T* start_lifetime_as( const void* p ) noexcept;
template< class T >
(3) (since C++23)
volatile T* start_lifetime_as( volatile void* p ) noexcept;
template< class T >
const volatile T* start_lifetime_as( const volatile void* p )
(4) (since C++23)
noexcept;
start_lifetime_as_array
template< class T >
(5) (since C++23)
T* start_lifetime_as_array( void* p, std::size_t n ) noexcept;
template< class T >
const T* start_lifetime_as_array( const void* p,
(6) (since C++23)
std::size_t n ) noexcept;
template< class T >
volatile T* start_lifetime_as_array( volatile void* p,
(7) (since C++23)
std::size_t n ) noexcept;
template< class T >
const volatile T* start_lifetime_as_array( const volatile void* p,
(8) (since C++23)
std::size_t n )
noexcept;
so we've got overloads (1)β(4) for start_lifetime_as and (5)β(8) for start_lifetime_as_array, all (since C++23) and all noexcept β slay β¨
π Parameters π
pβ the address of the region consisting objectsnβ the number of the element of the 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 top.
π Notes π
new (void_ptr) unsigned char[size]ornew (void_ptr) std::byte[size]works as an untyped version ofstd::start_lifetime_as, but it does not keep the object representation.std::start_lifetime_ashandles non-array types as well as arrays of known bound, whilestd::start_lifetime_as_arrayhandles arrays of unknown bound.
π Feature-test macro π
- Feature-test macro:
__cpp_lib_start_lifetime_asβ Value:202207Lβ Std:(C++23)β Feature:Explicit lifetime management
π§ͺ Example π§ͺ
okay so run this code, bestie β it's giving "look at the lifetime glow-up" π
// Run this code
#include <complex>
#include <iostream>
#include <memory>
int main()
{
alignas(std::complex<float>) unsigned char
network_data[sizeof(std::complex<float>)]{
0xcd, 0xcc, 0xcc, 0x3d, 0xcd, 0xcc, 0x4c, 0x3e
};
// auto d = *reinterpret_cast<std::complex<float>*>(network_data);
// std::cout << d << '\n'; // UB: network_data does not point to a
complex<float>
// auto d =
*std::launder(reinterpret_cast<std::complex<float>*>(network_data));
// std::cout << d << '\n'; // Possible UB, related to CWG1997:
// the implicitly created complex<float> may hold indeterminate
value
auto d =
*std::start_lifetime_as<std::complex<float>>(network_data);
std::cout << d << '\n'; // OK
}
π Possible output π
(0.1,0.2)
π References π
- C++23 standard (
ISO/IEC 14882:2023):20.2.6Explicit lifetime management[obj.lifetime]
π See also π
bit_cast(C++20)β reinterpret the object representation of one type as that of another (function template)as_bytes,as_writable_bytes(C++20)β converts a span into a view of its underlying bytes (function template)
mwah π β source: http://cppreference.com; version/date: 2024.06.10; page: std::start_...ime_as_array(3) π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms