1#ifndef NEFORCE_CORE_MEMORY_CONSTRUCT_HPP__
2#define NEFORCE_CORE_MEMORY_CONSTRUCT_HPP__
13NEFORCE_BEGIN_NAMESPACE__
32template <
typename T,
typename... Args>
34 static_assert(
is_constructible_v<T, Args...>,
"T must be constructible with arguments");
35 return static_cast<T*
>(
new (
static_cast<void*
>(ptr)) T(_NEFORCE
forward<Args>(args)...));
59template <
typename Iterator>
60NEFORCE_CONSTEXPR20 enable_if_t<is_iter_v<Iterator> && !is_trivially_destructible_v<iter_value_t<Iterator>>>
62 for (; first < last; ++first) {
77template <
typename Iterator>
78NEFORCE_CONSTEXPR20 enable_if_t<is_iter_v<Iterator> && is_trivially_destructible_v<iter_value_t<Iterator>>>
79destroy(Iterator first, Iterator last)
noexcept {
85struct temporary_guard {
86 static_assert(is_nothrow_move_constructible_v<T>,
"T must be nothrow move constructible");
93 constexpr explicit temporary_guard(T& value) :
94 guarded_ptr(_NEFORCE addressof(value)),
95 temp(_NEFORCE move(value)) {
96 _NEFORCE destroy(guarded_ptr);
99 NEFORCE_CONSTEXPR20 ~temporary_guard() {
105 temporary_guard(
const temporary_guard&) =
delete;
106 temporary_guard& operator=(
const temporary_guard&) =
delete;
108 constexpr T&&
release() noexcept {
109 guarded_ptr =
nullptr;
110 return _NEFORCE
move(temp);
114template <
typename NT,
typename OT,
typename Arg>
115constexpr enable_if_t<is_nothrow_constructible_v<NT, Arg>>
116reinitialize(NT* new_val, OT* old_val, Arg&& arg)
noexcept(is_nothrow_constructible_v<NT, Arg>) {
118 _NEFORCE
construct(new_val, _NEFORCE forward<Arg>(arg));
122template <
typename NT,
typename OT,
typename Arg>
123constexpr enable_if_t<!is_nothrow_constructible_v<NT, Arg> && is_nothrow_move_constructible_v<NT>>
124reinitialize(NT* new_val, OT* old_val, Arg&& arg)
noexcept(is_nothrow_constructible_v<NT, Arg>) {
125 NT temp(_NEFORCE forward<Arg>(arg));
131template <
typename NT,
typename OT,
typename Arg>
132constexpr enable_if_t<!is_nothrow_constructible_v<NT, Arg> && !is_nothrow_move_constructible_v<NT>>
133reinitialize(NT* new_val, OT* old_val, Arg&& arg)
noexcept(is_nothrow_constructible_v<NT, Arg>) {
134 temporary_guard<OT> guard(*old_val);
135 _NEFORCE
construct(new_val, _NEFORCE forward<Arg>(arg));
142NEFORCE_END_NAMESPACE__
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
constexpr T * construct(T *ptr, Args &&... args) noexcept(is_nothrow_constructible_v< T, Args... >)
在指定内存位置构造对象
constexpr void destroy(T *pointer) noexcept(is_nothrow_destructible_v< T >)
销毁单个对象
@ release
释放操作,确保前面的读写不会被重排到后面
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
constexpr bool is_constructible_v
is_constructible的便捷变量模板
constexpr bool is_nothrow_constructible_v
is_nothrow_constructible的便捷变量模板
constexpr bool is_nothrow_destructible_v
is_nothrow_destructible的便捷变量模板