guanaqo 1.0.0-alpha.24
Utilities for scientific software
Loading...
Searching...
No Matches
lifetime.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @ingroup memory
5/// Fallback implementations for `std::start_lifetime_as`.
6
7#include <memory>
8
9#if __cpp_lib_start_lifetime_as >= 202207L
10
11namespace guanaqo {
12using std::start_lifetime_as;
13using std::start_lifetime_as_array;
14} // namespace guanaqo
15
16#else
17
18#include <cstring>
19#include <new>
20#include <type_traits>
21
22namespace guanaqo {
23
24/// @addtogroup memory
25/// @{
26
27template <class T>
28 requires std::is_trivially_copyable_v<T>
29T *start_lifetime_as_array(void *p, size_t n) noexcept {
30#if __cpp_lib_is_implicit_lifetime >= 202302L
31 static_assert(std::is_implicit_lifetime_v<T>);
32#endif
33 return std::launder(static_cast<T *>(std::memmove(p, p, n * sizeof(T))));
34}
35
36template <class T>
37 requires std::is_trivially_copyable_v<T>
38const T *start_lifetime_as_array(const void *p, size_t n) noexcept {
39#if __cpp_lib_is_implicit_lifetime >= 202302L
40 static_assert(std::is_implicit_lifetime_v<T>);
41#endif
42 static_cast<void>(n); // TODO
43 // best we can do without compiler support
44 return std::launder(static_cast<const T *>(p));
45}
46
47template <class T>
48 requires std::is_trivially_copyable_v<T>
49T *start_lifetime_as(void *p) noexcept {
50 return start_lifetime_as_array<T>(p, 1);
51}
52
53template <class T>
54 requires std::is_trivially_copyable_v<T>
55const T *start_lifetime_as(const void *p) noexcept {
56 return start_lifetime_as_array<T>(p, 1);
57}
58
59/// @}
60
61} // namespace guanaqo
62
63#endif
T * start_lifetime_as_array(void *p, size_t n) noexcept
Definition lifetime.hpp:29
T * start_lifetime_as(void *p) noexcept
Definition lifetime.hpp:49