std::start_...ime_as_array(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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 objects
  • n β€” 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 to p.

πŸ’œ Notes πŸ’œ

  • new (void_ptr) unsigned char[size] or new (void_ptr) std::byte[size] works as an untyped version of std::start_lifetime_as, but it does not keep the object representation.
  • std::start_lifetime_as handles non-array types as well as arrays of known bound, while std::start_lifetime_as_array handles 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.6 Explicit 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