From 8ff5798f1cfb93b242a526a1b576047570d2ab3d Mon Sep 17 00:00:00 2001 From: Mingxin Wang Date: Wed, 18 Nov 2026 22:42:49 +0800 Subject: [PATCH] Support facades that forbid destruction - Added a deleted destructor for `F::destructibility == lifetime_requirement::none`, because `proxy` was ill-formed for such a facade when no destructor could be selected. It is declared before the other destructors, because NVHPC 26.3 otherwise selected it while checking `*this = proxy{rhs}` in a member template and rejected code that never used such a facade. - Stopped delegating in `proxy(std::nullptr_t)`, because GCC, Clang and MSVC reject a delegating constructor of a class whose destructor is deleted. The destructor would run if the delegating constructor exited via an exception ([except.ctor]/4). - Excluded the relocating `swap` when destruction is forbidden, because it needs a temporary `proxy`. - Changed the constraints of the pointer and in-place constructors from `std::is_constructible_v` to the placement *new-expression* in the constraints of `std::construct_at`, and their `noexcept` specifications from `std::is_nothrow_constructible_v` to whether that expression is non-throwing, because the traits also require a destructor, so a pointer that is not destructible could not be stored when `F::destructibility == lifetime_requirement::none`. - Changed the `supported` and `nothrow` copyability requirements on the contained type, and the `noexcept` specification of the internal copy, to the same expression with `const P&`, because `std::is_copy_constructible_v` and `std::is_nothrow_copy_constructible_v` also require a destructor, so a copyable facade could not store a pointer that is not destructible, and a pointer whose destructor may throw could not meet `nothrow`. - Defined the exposition-only concepts *constructible* and *nothrow-constructible* in the *ProxyFacade* requirements, with the reason they differ from the standard traits, and used them in the new copyability mapping there and in the constructor synopsis, so that neither spells out the *new-expression*. - Kept `emplace` and the pointer assignment on the traits, because they require a destructible facade, so the contained type is destructible there. - Added static assertions for the deleted destructor, the `swap` constraint, and the constraint and the `noexcept` specification of each constructor, requirement checks for the `supported` and `nothrow` copyability of a pointer that is not destructible or whose destructor may throw, a test that constructs such a `proxy` with a *new-expression*, and a test that stores and copies a non-destructible pointer. --- docs/spec/ProxyFacade.md | 24 +++++++ docs/spec/proxy/constructor.md | 15 ++-- docs/spec/proxy/destructor.md | 4 +- docs/spec/proxy/swap.md | 6 +- include/proxy/v5/detail/core.h | 42 ++++++++---- tests/proxy_lifetime_tests.cpp | 121 +++++++++++++++++++++++++++++++++ tests/proxy_traits_tests.cpp | 35 ++++++++++ 7 files changed, 223 insertions(+), 24 deletions(-) diff --git a/docs/spec/ProxyFacade.md b/docs/spec/ProxyFacade.md index 423f2410..91819a73 100644 --- a/docs/spec/ProxyFacade.md +++ b/docs/spec/ProxyFacade.md @@ -23,8 +23,32 @@ A type `F` meets the *ProxyFacade* requirements of a type `P` and a type `MP`, w In other words, `P` either supports dereferencing or provides an `element_type`, and `std::pointer_traits

` yields a valid `element_type`. +The copyability requirements below and the constructors of [`proxy`](proxy/constructor.md) make use of the following exposition-only concepts: + +```cpp +template +concept constructible = requires { + ::new (std::declval()) T(std::declval()...); +}; // exposition only + +template +concept nothrow-constructible = constructible && + noexcept(::new (std::declval()) T(std::declval()...)); // exposition only +``` + +*constructible* is the condition under which [`std::construct_at`](https://en.cppreference.com/w/cpp/memory/construct_at) participates in overload resolution. Unlike `std::is_constructible_v`, `std::is_nothrow_constructible_v`, and `std::constructible_from`, these concepts do not involve the destructor of `T`. Therefore, a `P` that is not destructible can be stored when `F::destructibility` is `lifetime_requirement::none` and `P` meets the relocatability requirement below. By default, that means `F::relocatability` is also `lifetime_requirement::none`, because every other value requires `P` to be destructible unless [`is_bitwise_trivially_relocatable`](is_bitwise_trivially_relocatable.md) is specialized for `P`. Such a `P` can be copied when `F::copyability` is `lifetime_requirement::supported` or `lifetime_requirement::nothrow`, but not `lifetime_requirement::trivial`, because `std::is_trivially_copy_constructible_v

` is `false` for a `P` that is not destructible. A `P` whose destructor may throw can meet `lifetime_requirement::nothrow` copyability. + ## Notes +Copyability is defined as *copy-construct an object at a given address*. Specifically, the value of `F::copyability` maps to the following requirements on `P`: + +| Value | Requirement on `P` | +| --------------------------------- | ------------------------------------------------------------ | +| `lifetime_requirement::none` | None | +| `lifetime_requirement::supported` | `constructible` | +| `lifetime_requirement::nothrow` | `nothrow-constructible` | +| `lifetime_requirement::trivial` | `std::is_trivially_copy_constructible_v

` | + Relocatability is defined as *move-construct an object and then destroy the original instance*. Specifically, the value of `F::relocatability` maps to the following requirements on `P`: | Value | Requirement on `P` | diff --git a/docs/spec/proxy/constructor.md b/docs/spec/proxy/constructor.md index 59cedad2..d8bd331d 100644 --- a/docs/spec/proxy/constructor.md +++ b/docs/spec/proxy/constructor.md @@ -34,24 +34,25 @@ proxy(proxy&& rhs) // (6) template -proxy(P&& ptr) noexcept(std::is_nothrow_constructible_v, P>) - requires(std::is_constructible_v, P>); +proxy(P&& ptr) noexcept(nothrow-constructible, P>) + requires(constructible, P>); // (7) template explicit proxy(std::in_place_type_t

, Args&&... args) - noexcept(std::is_nothrow_constructible_v) - requires(std::is_constructible_v); + noexcept(nothrow-constructible) + requires(constructible); // (8) template explicit proxy(std::in_place_type_t

, std::initializer_list il, Args&&... args) - noexcept(std::is_nothrow_constructible_v< - P, std::initializer_list&, Args...>) - requires(std::is_constructible_v&, Args...>); + noexcept(nothrow-constructible&, Args...>) + requires(constructible&, Args...>); ``` +See [*ProxyFacade* requirements](../ProxyFacade.md) for the definitions of the exposition-only concepts *constructible* and *nothrow-constructible*. + Creates a new `proxy`. - `(1)` Default constructor and the constructor taking `nullptr` construct a `proxy` that does not contain a value. diff --git a/docs/spec/proxy/destructor.md b/docs/spec/proxy/destructor.md index da3bbfc5..14b16f39 100644 --- a/docs/spec/proxy/destructor.md +++ b/docs/spec/proxy/destructor.md @@ -6,9 +6,11 @@ ~proxy() noexcept(F::destructibility == lifetime_requirement::nothrow) requires(F::destructibility == lifetime_requirement::supported || F::destructibility == lifetime_requirement::nothrow); +~proxy() requires(F::destructibility == lifetime_requirement::none) + = delete; ``` -Destroys the `proxy` object. If the `proxy` contains a value, the contained value is also destroyed. When `F::destructibility == lifetime_requirement::trivial`, the destructor is defaulted. It is trivial only if the metadata storage and accessor base classes also have trivial destructors. +Destroys the `proxy` object. If the `proxy` contains a value, the contained value is also destroyed. When `F::destructibility == lifetime_requirement::trivial`, the destructor is defaulted. It is trivial only if the metadata storage and accessor base classes also have trivial destructors. When `F::destructibility == lifetime_requirement::none`, the destructor is deleted, so a variable or a function return value of this `proxy` type is ill-formed. Such a `proxy` can be created with a *new-expression*. ## Example diff --git a/docs/spec/proxy/swap.md b/docs/spec/proxy/swap.md index 845eb7f4..246f7fc1 100644 --- a/docs/spec/proxy/swap.md +++ b/docs/spec/proxy/swap.md @@ -5,8 +5,10 @@ void swap(proxy& rhs) noexcept(F::relocatability >= lifetime_requirement::nothrow || F::copyability == lifetime_requirement::trivial) - requires(F::relocatability >= lifetime_requirement::supported || - F::copyability == lifetime_requirement::trivial); + requires(F::relocatability == lifetime_requirement::trivial || + F::copyability == lifetime_requirement::trivial || + (F::relocatability >= lifetime_requirement::supported && + F::destructibility >= lifetime_requirement::supported)); // (2) (since 5.0.0) void swap(proxy& rhs) diff --git a/include/proxy/v5/detail/core.h b/include/proxy/v5/detail/core.h index 5714fc16..9a87790a 100644 --- a/include/proxy/v5/detail/core.h +++ b/include/proxy/v5/detail/core.h @@ -196,16 +196,25 @@ struct add_qualifier_traits template using add_qualifier_t = add_qualifier_traits::type; +// Like std::is_constructible_v, but does not require T to be destructible. +template +concept constructible = + requires { ::new (std::declval()) T(std::declval()...); }; +template +concept nothrow_constructible = + constructible && + noexcept(::new (std::declval()) T(std::declval()...)); + template struct copyability_traits : inapplicable_traits {}; template struct copyability_traits : applicable_traits {}; template - requires(std::is_copy_constructible_v) + requires(constructible) struct copyability_traits : applicable_traits {}; template - requires(std::is_nothrow_copy_constructible_v) + requires(nothrow_constructible) struct copyability_traits : applicable_traits {}; template @@ -553,8 +562,8 @@ consteval bool is_metadata_well_formed() { } struct copy_dispatch { template - PRO5D_STATIC_CALL(void, const T& self, void* rhs) noexcept( - std::is_nothrow_copy_constructible_v) { + PRO5D_STATIC_CALL(void, const T& self, + void* rhs) noexcept(nothrow_constructible) { std::construct_at(static_cast(rhs), self); } }; @@ -1170,7 +1179,7 @@ class proxy : public detail::proxy_traits::direct_accessor, using metadata_policy_type = MP; proxy() noexcept { initialize(); } - proxy(std::nullptr_t) noexcept : proxy() {} + proxy(std::nullptr_t) noexcept { initialize(); } proxy(const proxy&) noexcept requires(F::copyability == lifetime_requirement::trivial) = default; @@ -1212,28 +1221,28 @@ class proxy : public detail::proxy_traits::direct_accessor, } template constexpr proxy(P&& ptr) noexcept( - std::is_nothrow_constructible_v, P>) + detail::nothrow_constructible, P>) requires(!detail::specialization_of, proxy> && detail::pointer_like> && - std::is_constructible_v, P>) + detail::constructible, P>) { initialize>(std::forward

(ptr)); } template constexpr explicit proxy(std::in_place_type_t

, Args&&... args) noexcept( - std::is_nothrow_constructible_v) - requires(detail::pointer_like

&& std::is_constructible_v) + detail::nothrow_constructible) + requires(detail::pointer_like

&& detail::constructible) { initialize

(std::forward(args)...); } template constexpr explicit proxy( std::in_place_type_t

, std::initializer_list il, - Args&&... args) noexcept(std:: - is_nothrow_constructible_v< + Args&&... args) noexcept(detail:: + nothrow_constructible< P, std::initializer_list&, Args...>) requires(detail::pointer_like

&& - std::is_constructible_v&, Args...>) + detail::constructible&, Args...>) { initialize

(il, std::forward(args)...); } @@ -1339,6 +1348,9 @@ class proxy : public detail::proxy_traits::direct_accessor, } return *this; } + ~proxy() + requires(F::destructibility == lifetime_requirement::none) + = delete; ~proxy() requires(F::destructibility == lifetime_requirement::trivial) = default; @@ -1361,8 +1373,10 @@ class proxy : public detail::proxy_traits::direct_accessor, lifetime_requirement::nothrow || F::copyability == lifetime_requirement::trivial) - requires(F::relocatability >= lifetime_requirement::supported || - F::copyability == lifetime_requirement::trivial) + requires(F::relocatability == lifetime_requirement::trivial || + F::copyability == lifetime_requirement::trivial || + (F::relocatability >= lifetime_requirement::supported && + F::destructibility >= lifetime_requirement::supported)) { if constexpr (F::relocatability == lifetime_requirement::trivial || F::copyability == lifetime_requirement::trivial) { diff --git a/tests/proxy_lifetime_tests.cpp b/tests/proxy_lifetime_tests.cpp index d626e8dc..cf02f887 100644 --- a/tests/proxy_lifetime_tests.cpp +++ b/tests/proxy_lifetime_tests.cpp @@ -3,8 +3,12 @@ // Licensed under the MIT License. #include "utils.h" +#include #include +#include +#include #include +#include namespace proxy_lifetime_tests_detail { @@ -49,6 +53,101 @@ static_assert(!std::is_convertible_v, static_assert(!std::is_convertible_v, pro::proxy>); +struct TestNonDestructibleFacade + : pro::facade_builder // + ::add_convention // + ::support_relocation // + ::support_destruction // + ::build {}; + +struct TestNonDestructibleTrivialRelocationFacade + : pro::facade_builder // + ::add_convention // + ::support_relocation // + ::support_destruction // + ::build {}; + +struct TestNonDestructibleRelocationFacade + : pro::facade_builder // + ::add_convention // + ::support_relocation // + ::support_destruction // + ::build {}; + +template +concept MemberSwappable = requires(P& lhs, P& rhs) { lhs.swap(rhs); }; + +static_assert(!std::is_destructible_v>); +static_assert( + MemberSwappable>); +static_assert( + !MemberSwappable>); + +class NonDestructiblePtr { +public: + using element_type = int; + + explicit NonDestructiblePtr(int* ptr) noexcept : ptr_(ptr) {} + NonDestructiblePtr(std::initializer_list, int* ptr) noexcept + : ptr_(ptr) {} + NonDestructiblePtr(const NonDestructiblePtr&) noexcept = default; + ~NonDestructiblePtr() = delete; + int& operator*() const noexcept { return *ptr_; } + +private: + int* ptr_; +}; + +class ThrowingDestructionPtr { +public: + using element_type = int; + + explicit ThrowingDestructionPtr(int* ptr) noexcept : ptr_(ptr) {} + ThrowingDestructionPtr(std::initializer_list, int* ptr) noexcept + : ptr_(ptr) {} + ThrowingDestructionPtr(const ThrowingDestructionPtr&) noexcept = default; + ~ThrowingDestructionPtr() noexcept(false) {} + int& operator*() const noexcept { return *ptr_; } + +private: + int* ptr_; +}; + +template +concept NewConstructible = + requires { ::new (std::declval()) T(std::declval()...); }; + +template +concept NothrowNewConstructible = requires { + { ::new (std::declval()) T(std::declval()...) } noexcept; +}; + +static_assert(NewConstructible, + const NonDestructiblePtr&>); +static_assert(NewConstructible, + std::in_place_type_t, int*>); +static_assert(NewConstructible, + std::in_place_type_t, + std::initializer_list, int*>); + +static_assert(NothrowNewConstructible, + const ThrowingDestructionPtr&>); +static_assert(NothrowNewConstructible< + pro::proxy, + std::in_place_type_t, int*>); +static_assert( + NothrowNewConstructible, + std::in_place_type_t, + std::initializer_list, int*>); + +struct TestNonDestructibleCopyFacade + : pro::facade_builder // + ::add_convention // + ::support_copy // + ::support_relocation // + ::support_destruction // + ::build {}; + } // namespace proxy_lifetime_tests_detail namespace detail = proxy_lifetime_tests_detail; @@ -1589,6 +1688,28 @@ TEST(ProxyLifetimeTests, Test_ConvertingCopyAssignment_NoRelocation) { ASSERT_EQ(ToString(*p2), "Session 2"); } +TEST(ProxyLifetimeTests, Test_NonDestructible_Construction) { + using Proxy = pro::proxy; + alignas(Proxy) std::byte null_storage[sizeof(Proxy)]; + alignas(Proxy) std::byte value_storage[sizeof(Proxy)]; + Proxy* p1 = ::new (static_cast(null_storage)) Proxy(nullptr); + ASSERT_FALSE(p1->has_value()); + int v = 123; + Proxy* p2 = ::new (static_cast(value_storage)) Proxy(&v); + ASSERT_EQ(ToString(**p2), "123"); +} + +TEST(ProxyLifetimeTests, Test_NonDestructible_NonDestructiblePtr) { + using Proxy = pro::proxy; + alignas(Proxy) std::byte storage1[sizeof(Proxy)]; + alignas(Proxy) std::byte storage2[sizeof(Proxy)]; + int v = 123; + Proxy* p1 = ::new (static_cast(storage1)) + Proxy(std::in_place_type, &v); + Proxy* p2 = ::new (static_cast(storage2)) Proxy(*p1); + ASSERT_EQ(ToString(**p2), "123"); +} + TEST(ProxyLifetimeTests, Test_Substitution_TrivialDerived) { struct Super : pro::facade_builder // ::add_convention; using MockFunctionPtr = void (*)(); +struct MockNonDestructiblePtr { + using element_type = MockNonDestructiblePtr; + + MockNonDestructiblePtr(const MockNonDestructiblePtr&) noexcept = default; + ~MockNonDestructiblePtr() = delete; + const MockNonDestructiblePtr* operator->() const noexcept { return this; } +}; + +struct MockThrowingDestructionPtr { + using element_type = MockThrowingDestructionPtr; + + MockThrowingDestructionPtr(const MockThrowingDestructionPtr&) noexcept = + default; + ~MockThrowingDestructionPtr() noexcept(false) {} + const MockThrowingDestructionPtr* operator->() const noexcept { return this; } +}; + } // namespace proxy_traits_tests_detail namespace pro { @@ -213,6 +230,24 @@ static_assert( std::is_assignable_v, MockFunctionPtr>); static_assert(sizeof(pro::proxy) == 2 * sizeof(void*)); +struct NonDestructibleCopyableFacade + : pro::facade_builder // + ::support_copy // + ::support_relocation // + ::support_destruction // + ::build {}; +static_assert( + pro::proxiable); + +struct NothrowCopyableFacade + : pro::facade_builder // + ::support_copy // + ::support_relocation // + ::support_destruction // + ::build {}; +static_assert( + pro::proxiable); + struct TrivialFacade : pro::facade_builder // ::restrict_layout //