From a2b2cefd6a20e2e346663eb50c23abdb69f39151 Mon Sep 17 00:00:00 2001 From: Mingxin Wang Date: Sat, 21 Nov 2026 22:26:08 +0800 Subject: [PATCH] Relocate the contained value through the construction dispatch - Generalized the internal copy dispatch to forward its operand and renamed it `construction_dispatch`. Relocation now moves the contained value through the same dispatch as copying, so `relocate_dispatch` is no longer needed. - Narrowed the special case in the generated invocation function to the bitwise copy of a bitwise trivially relocatable type, which must not destroy the source. Other types reach the rvalue branch, which moves the value into the destination and then destroys the source. The immediately invoked lambda is gone. - Renamed `destroy_dispatch` to `destruction_dispatch` to match. - Defined both dispatches before the generated invocation function, so the special case names a complete type without a forward declaration. - Moved `destroying_guard` into the rvalue branch as a local class, because that branch is now its only user. --- include/proxy/v5/detail/core.h | 69 +++++++++++++++------------------- 1 file changed, 30 insertions(+), 39 deletions(-) diff --git a/include/proxy/v5/detail/core.h b/include/proxy/v5/detail/core.h index 8829e84..910dae4 100644 --- a/include/proxy/v5/detail/core.h +++ b/include/proxy/v5/detail/core.h @@ -360,16 +360,17 @@ R invoke_dispatch(Args&&... args) { #pragma warning(pop) #endif // defined(_MSC_VER) && !defined(__clang__) -template -struct destroying_guard { - explicit destroying_guard(P* p) noexcept : p_(p) {} - ~destroying_guard() noexcept(std::is_nothrow_destructible_v

) { p_->~P(); } - -private: - P* p_; +struct construction_dispatch { + template + PRO5D_STATIC_CALL(void, T&& self, void* rhs) noexcept( + nothrow_constructible, T>) { + std::construct_at(static_cast*>(rhs), + std::forward(self)); + } +}; +struct destruction_dispatch { + PRO5D_STATIC_CALL(void, auto&&) noexcept {} }; - -struct relocate_dispatch; template using erased_ptr_t = std::conditional_t ptr, Args... args) noexcept(NE) -> R { auto* self = std::launder( static_cast>*>(ptr)); - if constexpr (std::is_same_v) { - [self](void* rhs) { - if constexpr (is_bitwise_trivially_relocatable_v

) { - if constexpr (!std::is_empty_v

) { - std::uninitialized_copy_n(reinterpret_cast(self), - sizeof(P), static_cast(rhs)); - } - } else { - destroying_guard

guard{self}; - std::construct_at(static_cast(rhs), std::move(*self)); - } - }(args...); + if constexpr (std::is_same_v && + Q == qualifier_type::rv && + is_bitwise_trivially_relocatable_v

) { + if constexpr (!std::is_empty_v

) { + std::uninitialized_copy_n(reinterpret_cast(self), + sizeof(P), static_cast(args)...); + } } else if constexpr (Q == qualifier_type::rv) { - destroying_guard

guard{self}; + struct destroying_guard { + ~destroying_guard() noexcept(std::is_nothrow_destructible_v

) { + p->~P(); + } + P* p; + } guard{self}; return invoke_dispatch(get_operand(self), std::forward(args)...); } else { @@ -560,16 +561,6 @@ consteval bool is_metadata_well_formed() { } return false; } -struct copy_dispatch { - template - PRO5D_STATIC_CALL(void, const T& self, - void* rhs) noexcept(nothrow_constructible) { - std::construct_at(static_cast(rhs), self); - } -}; -struct destroy_dispatch { - PRO5D_STATIC_CALL(void, auto&&) noexcept {} -}; template struct lifetime_meta_traits : std::type_identity {}; template @@ -1031,12 +1022,12 @@ struct proxy_traits proxy_meta_base_t, composite_t< typename proxy_traits::super_meta, - lifetime_meta_t, - lifetime_meta_t, - lifetime_meta_t, + lifetime_meta_t, typename proxy_traits::self_op_meta, typename proxy_traits::refl_meta>>; @@ -1500,7 +1491,7 @@ class proxy : public detail::proxy_traits::direct_accessor, static_cast(detail::proxy_helper::get_ptr(rhs)), F2::max_size, ptr_); } else { - invoke(rhs, ptr_); } @@ -1520,7 +1511,7 @@ class proxy : public detail::proxy_traits::direct_accessor, F2::max_size, ptr_); } else { detail::proxy_helper::get_meta>(rhs) .function(rhs_ptr, ptr_); @@ -1547,7 +1538,7 @@ class proxy : public detail::proxy_traits::direct_accessor, { if constexpr (F::destructibility != lifetime_requirement::trivial) { if (meta_) { - invoke( std::move(*this));