1
0
forked from Alepha/Alepha

This should be in C++23, but some compilers/libraries still lack it.

This commit is contained in:
2023-11-10 23:15:37 -05:00
parent db70cb3b08
commit 0d66b5a135

39
lifetime.h Normal file
View File

@ -0,0 +1,39 @@
static_assert( __cplusplus > 2020'00 );
#pragma once
#include <Alepha/Alepha.h>
#include <cstddef>
#include <type_traits>
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;
}