diff --git a/lifetime.h b/lifetime.h new file mode 100644 index 0000000..aa44f0e --- /dev/null +++ b/lifetime.h @@ -0,0 +1,39 @@ +static_assert( __cplusplus > 2020'00 ); + +#pragma once + +#include + +#include + +#include + +namespace Alepha::Hydrogen ::detail:: lifetime_m +{ + inline namespace exports + { + // When C++23 arrives, just turn these into `using` statements. + + template< typename T > + std::add_pointer_t< T > + start_lifetime_as( void *const mp ) noexcept + { + const auto raw= new( mp ) std::byte[ sizeof( T ) ]; + const auto data= reinterpret_cast< std::add_pointer_t< T > >( raw ); + return std::launder( data ); + } + + template< typename T > + std::add_pointer_t< std::add_const_t< T > > + start_lifetime_as( const void *const p ) noexcept + { + const auto mp= const_cast< void * >( p ); + return start_lifetime_as< std::add_const_t< T > >( mp ); + } + } +} + +namespace Alepha::Hydrogen::inline exports::inline lifetime_m +{ + using namespace detail::lifetime_m::exports; +}