A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/algorithm/../ranges/../memory/start_lifetime_as.html below:

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

std::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)

std::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,

                                  std::size_t n ) noexcept;
(6) (since C++23) template< class T >

volatile T* start_lifetime_as_array( volatile void* p,

                                     std::size_t n ) noexcept;
(7) (since C++23) template< class T >

const volatile T* start_lifetime_as_array( const volatile void* p,

                                           std::size_t n ) noexcept;
(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.

5-8) Implicitly creates

an array with element type

T

and 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.

[edit] Parameters p - the address of the region consisting objects n - the number of the element of the array to be created [edit] 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.

[edit] 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.

[edit] Example
#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 d1 = *std::launder(reinterpret_cast<std::complex<float>*>(network_data));
//  std::cout << d1 << '\n'; // UB: implicitly created objects have dynamic storage
//                                  duration and have indeterminate value initially,
//                                  even when an array which provides storage for
//                                  them has determinate bytes.
//                                  See also CWG2721.
 
    auto d2 = *std::start_lifetime_as<std::complex<float>>(network_data);
    std::cout << d2 << '\n'; // OK
}

Possible output:

[edit] References
[edit] See also reinterpret the object representation of one type as that of another
(function template) [edit] converts a span into a view of its underlying bytes
(function template) [edit]

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4