[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/memory/start_lifetime_as [Back]  [Original]

std::start_lifetime_as, std::start_lifetime_as_array cppreference.com
cppreference.com

std::start_lifetime_as, std::start_lifetime_as_array

cppreference.com
 
C++
(C++20)
(C++20)
(C++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
 
no section name
no section name
(C++11)( C++23)
(C++11)( C++23)
(C++11)( C++23)
(C++11)( C++23)
(C++11)( C++23)
(C++11)( C++23)



no section name
 
<tbody> </tbody>
start_lifetime_as
template< class T > T* start_lifetime_as( void* p ) noexcept;
(1) ( C++23)
template< class T > const T* start_lifetime_as( const void* p ) noexcept;
(2) ( C++23)
template< class T > volatile T* start_lifetime_as( volatile void* p ) noexcept;
(3) ( C++23)
template< class T > const volatile T* start_lifetime_as( const volatile void* p ) noexcept;
(4) ( C++23)
start_lifetime_as_array
template< class T > T* start_lifetime_as_array( void* p, std::size_t n ) noexcept;
(5) ( C++23)
template< class T > const T* start_lifetime_as_array( const void* p, std::size_t n ) noexcept;
(6) ( C++23)
template< class T > volatile T* start_lifetime_as_array( volatile void* p, std::size_t n ) noexcept;
(7) ( C++23)
template< class T > const volatile T* start_lifetime_as_array( const volatile void* p, std::size_t n ) noexcept;
(8) ( C++23)
1-4) T ( p) . obj, TriviallyCopyable U, , std::bit_cast<U>(E) , , E U, obj. .
  • [p(char*)p + sizeof(T)) , , p,
  • T.
  • , .
5-8) T n. , n > 0 true, std::start_lifetime_as<U>(p), U " n T". .
  • T , .
  • , :
  • p T
  • n <= std::size_t(-1) / sizeof(T) false,
  • n > 0 [(char*)p(char*)p + (n * sizeof(T))) , , p.

p ,
n

1-4) , .
5-8) , ; , p.

new (void_ptr) unsigned char[size] new (void_ptr) std::byte[size] std::start_lifetime_as, .

std::start_lifetime_as , , , std::start_lifetime_as_array .

__cpp_lib_start_lifetime_as 202207L (C++23)

#include <complex>
#include <iostream>
#include <iterator>
#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'; //  : network_data  
                            //  complex<float>

//  auto d = *std::launder(reinterpret_cast<std::complex<float>*>(network_data));
//  std::cout << d << '\n'; //   ,   CWG1997:
//        complex<float>    

    auto d = *std::start_lifetime_as<std::complex<float>>(network_data);
    std::cout << d << '\n'; // OK
}

:

(0.1,0.2)

  • C++23 (ISO/IEC 14882:2023):
  • 20.2.6 [obj.lifetime]

(C++20)

( ) []
span ,
( ) []

Web Proxy Viewer  |  New URL  |  Original Page