alpaqa develop
Nonconvex constrained optimization
Loading...
Searching...
No Matches
lifetime.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4
5#if __cpp_lib_start_lifetime_as >= 202207L
6
7namespace alpaqa::util {
8using std::start_lifetime_as;
9using std::start_lifetime_as_array;
10} // namespace alpaqa::util
11
12#else
13
14#include <cstring>
15#include <new>
16#include <type_traits>
17
18namespace alpaqa::util {
19template <class T>
20 requires std::is_trivially_copyable_v<T>
21T *start_lifetime_as_array(void *p, size_t n) noexcept {
22#if __cpp_lib_is_implicit_lifetime >= 202302L
23 static_assert(std::is_implicit_lifetime_v<T>);
24#endif
25 return std::launder(static_cast<T *>(std::memmove(p, p, n * sizeof(T))));
26}
27template <class T>
28 requires std::is_trivially_copyable_v<T>
29const T *start_lifetime_as_array(const 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 static_cast<void>(n); // TODO
34 // best we can do without compiler support
35 return std::launder(static_cast<const T *>(p));
36}
37template <class T>
38 requires std::is_trivially_copyable_v<T>
39T *start_lifetime_as(void *p) noexcept {
40 return start_lifetime_as_array<T>(p, 1);
41}
42template <class T>
43 requires std::is_trivially_copyable_v<T>
44const T *start_lifetime_as(const void *p) noexcept {
45 return start_lifetime_as_array<T>(p, 1);
46}
47} // namespace alpaqa::util
48
49#endif
T * start_lifetime_as_array(void *p, size_t n) noexcept
Definition lifetime.hpp:21
T * start_lifetime_as(void *p) noexcept
Definition lifetime.hpp:39
constexpr const auto inf
Definition config.hpp:112