From 93b8b6f692cd8ad9061e765388c6a6acf31760db Mon Sep 17 00:00:00 2001 From: Nana Sakisaka <1901813+saki7@users.noreply.github.com> Date: Sat, 14 Nov 2026 06:25:32 +0900 Subject: [PATCH 1/2] MSVC's Nov. 2026 frontend robustness fix --- include/iris/indirect.hpp | 35 +++------------------ include/iris/marshal/adapt.hpp | 3 +- include/iris/rvariant/recursive_wrapper.hpp | 9 +++--- 3 files changed, 11 insertions(+), 36 deletions(-) diff --git a/include/iris/indirect.hpp b/include/iris/indirect.hpp index 9572429f..112670bd 100644 --- a/include/iris/indirect.hpp +++ b/include/iris/indirect.hpp @@ -54,7 +54,7 @@ class scoped_allocation // Polyfill for the C++26 `std::indirect` // https://eel.is/c++draft/indirect -template> +template> class indirect_base { static_assert(std::is_object_v); @@ -90,9 +90,7 @@ class indirect_base constexpr indirect_base(std::allocator_arg_t, Allocator const& a, indirect_base const& other) : alloc_(a) , ptr_(other.ptr_ ? this->make_obj(std::as_const(*other.ptr_)) : nullptr) - { - static_assert(std::is_copy_constructible_v); - } + {} constexpr indirect_base(indirect_base&& other) noexcept : alloc_(std::move(other.alloc_)) @@ -169,9 +167,6 @@ class indirect_base constexpr indirect_base& operator=(indirect_base const& other) { - static_assert(std::is_copy_assignable_v); - static_assert(std::is_copy_constructible_v); - if (std::addressof(other) == this) [[unlikely]] { return *this; } @@ -251,8 +246,6 @@ class indirect_base std::allocator_traits::is_always_equal::value ) { - static_assert(std::is_move_constructible_v); - if (std::addressof(other) == this) [[unlikely]] { return *this; } @@ -375,10 +368,10 @@ class indirect_base // Polyfill for the C++26 `std::indirect` // https://eel.is/c++draft/indirect template> -class indirect : public detail::indirect_base +class indirect : public detail::indirect_base, T, Allocator> { public: - using detail::indirect_base::indirect_base; + using detail::indirect_base::indirect_base; }; template @@ -458,12 +451,9 @@ template } // iris - -namespace std { - template requires ::iris::is_hash_enabled_v -struct hash<::iris::indirect> +struct std::hash<::iris::indirect> { [[nodiscard]] static size_t operator()(::iris::indirect const& obj) noexcept(::iris::is_nothrow_hashable_v) @@ -476,19 +466,4 @@ struct hash<::iris::indirect> } }; -} // std - - -namespace iris { - -template - requires is_hash_enabled_v -[[nodiscard]] std::size_t hash_value(indirect const& obj) - noexcept(is_nothrow_hashable_v) -{ - return std::hash>{}(obj); -} - -} // iris - #endif diff --git a/include/iris/marshal/adapt.hpp b/include/iris/marshal/adapt.hpp index 5e2fa764..da28adcd 100644 --- a/include/iris/marshal/adapt.hpp +++ b/include/iris/marshal/adapt.hpp @@ -8,7 +8,8 @@ #include // IWYU pragma: keep #include // IWYU pragma: keep -#include // IWYU pragma: export +#include // IWYU pragma: keep +#include // IWYU pragma: keep #include #include diff --git a/include/iris/rvariant/recursive_wrapper.hpp b/include/iris/rvariant/recursive_wrapper.hpp index 05c7896d..1c39ac9e 100644 --- a/include/iris/rvariant/recursive_wrapper.hpp +++ b/include/iris/rvariant/recursive_wrapper.hpp @@ -27,7 +27,7 @@ class recursive_wrapper_alloca; // This class covers 99.99% of recursive-variant use cases. template class recursive_wrapper - : private detail::indirect_base> + : private detail::indirect_base, T, std::allocator> { static_assert( !is_ttp_specialization_of_v && !is_ttp_specialization_of_v, @@ -38,7 +38,7 @@ class recursive_wrapper // If any changes are required, modify `recursive_wrapper_alloca` first. // See https://github.com/iris-cpp/iris/issues/43 for rationale - using base_type = detail::indirect_base>; + using base_type = detail::indirect_base>; public: using typename base_type::allocator_type; @@ -95,7 +95,6 @@ class recursive_wrapper //using base_type::operator=; constexpr recursive_wrapper& operator=(recursive_wrapper const&) = default; - constexpr recursive_wrapper& operator=(recursive_wrapper&&) noexcept = default; // This is required for proper delegation; otherwise constructor will be called @@ -127,14 +126,14 @@ class recursive_wrapper // "Allocator-aware" recursive_wrapper template> class recursive_wrapper_alloca - : private detail::indirect_base + : private detail::indirect_base, T, Allocator> { static_assert( !is_ttp_specialization_of_v && !is_ttp_specialization_of_v, "recursive wrapper of recursive wrapper is not allowed" ); - using base_type = detail::indirect_base; + using base_type = detail::indirect_base; public: using typename base_type::allocator_type; From 3cd985d437b05fb8649da0bf0540db1a7aeacaa3 Mon Sep 17 00:00:00 2001 From: Nana Sakisaka <1901813+saki7@users.noreply.github.com> Date: Sat, 14 Nov 2026 06:35:17 +0900 Subject: [PATCH 2/2] Split `indirect_base` into bits header --- include/iris/bits/indirect_base.hpp | 368 ++++++++++++++++++++ include/iris/indirect.hpp | 351 +------------------ include/iris/rvariant/recursive_wrapper.hpp | 45 +-- 3 files changed, 374 insertions(+), 390 deletions(-) create mode 100644 include/iris/bits/indirect_base.hpp diff --git a/include/iris/bits/indirect_base.hpp b/include/iris/bits/indirect_base.hpp new file mode 100644 index 00000000..e3e2642e --- /dev/null +++ b/include/iris/bits/indirect_base.hpp @@ -0,0 +1,368 @@ +#ifndef IRIS_ZZ_BITS_INDIRECT_BASE_HPP +#define IRIS_ZZ_BITS_INDIRECT_BASE_HPP + +// SPDX-License-Identifier: MIT + +#include + +#include + +#include +#include +#include +#include + +#include + +namespace iris { + +namespace detail { + +template +class scoped_allocation +{ +public: + using pointer = std::allocator_traits::pointer; + using const_pointer = std::allocator_traits::const_pointer; + + template + constexpr explicit scoped_allocation(Alloc const& a, Args&&... args) + : alloc_(a) + , ptr_(std::allocator_traits::allocate(alloc_, 1)) + { + std::allocator_traits::construct(alloc_, this->get(), std::forward(args)...); + } + + constexpr ~scoped_allocation() + noexcept(noexcept(std::allocator_traits::deallocate(alloc_, ptr_, 1))) + { + if (ptr_) [[unlikely]] { + std::allocator_traits::deallocate(alloc_, ptr_, 1); + } + } + + [[nodiscard]] constexpr Alloc get_allocator() const noexcept { return alloc_; } + [[nodiscard]] constexpr auto get() const noexcept { return std::to_address(ptr_); } + [[nodiscard]] constexpr pointer release() noexcept { return std::exchange(ptr_, nullptr); } + +private: + IRIS_NO_UNIQUE_ADDRESS Alloc alloc_; + pointer ptr_; +}; + +// Polyfill for the C++26 `std::indirect` +// https://eel.is/c++draft/indirect +template> +class indirect_base +{ + static_assert(std::is_object_v); + static_assert(!std::is_array_v); + static_assert(!std::is_same_v); + static_assert(!is_ttp_specialization_of_v); + static_assert(!std::is_const_v && !std::is_volatile_v); + static_assert(std::is_same_v::value_type>); + +public: + using value_type = T; + using allocator_type = Allocator; + using pointer = std::allocator_traits::pointer; + using const_pointer = std::allocator_traits::const_pointer; + + constexpr explicit indirect_base() requires std::is_default_constructible_v + : ptr_(this->make_obj()) + {} + + constexpr indirect_base(indirect_base const& other) + : indirect_base( + std::allocator_arg, + std::allocator_traits::select_on_container_copy_construction(other.alloc_), + other + ) + {} + + constexpr explicit indirect_base(std::allocator_arg_t, Allocator const& a) + : alloc_(a) + , ptr_(this->make_obj()) + {} + + constexpr indirect_base(std::allocator_arg_t, Allocator const& a, indirect_base const& other) + : alloc_(a) + , ptr_(other.ptr_ ? this->make_obj(std::as_const(*other.ptr_)) : nullptr) + {} + + constexpr indirect_base(indirect_base&& other) noexcept + : alloc_(std::move(other.alloc_)) + , ptr_(std::exchange(other.ptr_, nullptr)) + {} + + constexpr indirect_base(std::allocator_arg_t, Allocator const& a, indirect_base&& other) + noexcept(std::allocator_traits::is_always_equal::value) + : alloc_(a) + , ptr_(alloc_ == other.alloc_ + ? std::exchange(other.ptr_, nullptr) + : this->make_obj(*std::move(other)) + ) + {} + + template + requires + (!std::is_same_v, indirect_base>) && + (!std::is_same_v, std::in_place_t>) && + std::is_constructible_v && + std::is_default_constructible_v + constexpr explicit indirect_base(U&& u) + : ptr_(this->make_obj(std::forward(u))) + {} + + template + requires + (!std::is_same_v, indirect_base>) && + (!std::is_same_v, std::in_place_t>) && + std::is_constructible_v + constexpr explicit indirect_base(std::allocator_arg_t, Allocator const& a, U&& u) + : alloc_(a) + , ptr_(this->make_obj(std::forward(u))) + {} + + template + requires + std::is_constructible_v && + std::is_default_constructible_v + constexpr explicit indirect_base(std::in_place_t, Us&&... us) + : ptr_(this->make_obj(std::forward(us)...)) + {} + + template + requires + std::is_constructible_v + constexpr explicit indirect_base(std::allocator_arg_t, Allocator const& a, std::in_place_t, Us&&... us) + : alloc_(a) + , ptr_(this->make_obj(std::forward(us)...)) + {} + + template + requires + std::is_constructible_v&, Us...> && + std::is_default_constructible_v + constexpr explicit indirect_base(std::in_place_t, std::initializer_list il, Us&&... us) + : ptr_(this->make_obj(il, std::forward(us)...)) + {} + + template + requires + std::is_constructible_v&, Us...> + constexpr explicit indirect_base(std::allocator_arg_t, Allocator const& a, std::in_place_t, std::initializer_list il, Us&&... us) + : alloc_(a) + , ptr_(this->make_obj(il, std::forward(us)...)) + {} + + constexpr ~indirect_base() noexcept + { + if (ptr_) [[likely]] { + this->destroy_deallocate(); + } + } + + constexpr indirect_base& operator=(indirect_base const& other) + { + if (std::addressof(other) == this) [[unlikely]] { + return *this; + } + + constexpr bool pocca = std::allocator_traits::propagate_on_container_copy_assignment::value; + + if (other.ptr_) [[likely]] { + if constexpr (std::allocator_traits::is_always_equal::value) { // NOLINT(bugprone-branch-clone) + if (ptr_) [[likely]] { + // both contain value and allocator is equal; copy assign + **this = *other; + if constexpr (pocca) { + alloc_ = other.alloc_; + } + return *this; + } + + assert(other.ptr_ && !ptr_); + if constexpr (pocca) { + ptr_ = other.make_obj(*other); + alloc_ = other.alloc_; + } else { + ptr_ = this->make_obj(*other); + } + return *this; + + } else if (alloc_ == other.alloc_) { + if (ptr_) [[likely]] { + // both contain value and allocator is equal; copy assign + **this = *other; + if constexpr (pocca) { + alloc_ = other.alloc_; + } + return *this; + } + + assert(other.ptr_ && !ptr_); + if constexpr (pocca) { + ptr_ = other.make_obj(*other); + alloc_ = other.alloc_; + } else { + ptr_ = this->make_obj(*other); + } + return *this; + + } else { + // other.ptr_ && (ptr_ || !ptr_) + // either allocator is not equal or `this` does not contain value; create copy from `other` + if (ptr_) [[likely]] { + this->destroy_deallocate(); + ptr_ = nullptr; // make it safer + } + if constexpr (pocca) { + ptr_ = other.make_obj(*other); + alloc_ = other.alloc_; + } else { + ptr_ = this->make_obj(*other); + } + return *this; + } + + } else [[unlikely]] { // !other.ptr_ + if (ptr_) [[likely]] { + this->destroy_deallocate(); + ptr_ = nullptr; // make it safer + } + if constexpr (pocca) { + alloc_ = other.alloc_; + } + return *this; + } + } + + constexpr indirect_base& operator=(indirect_base&& other) + noexcept( + std::allocator_traits::propagate_on_container_move_assignment::value || + std::allocator_traits::is_always_equal::value + ) + { + if (std::addressof(other) == this) [[unlikely]] { + return *this; + } + + constexpr bool pocma = std::allocator_traits::propagate_on_container_move_assignment::value; + + if (other.ptr_) [[likely]] { + if constexpr (std::allocator_traits::is_always_equal::value) { + if (ptr_) [[likely]] { + this->destroy_deallocate(); + } + ptr_ = std::exchange(other.ptr_, nullptr); + if constexpr (pocma) { + alloc_ = other.alloc_; + } + return *this; + + } else if (alloc_ == other.alloc_) { + if (ptr_) [[likely]] { + this->destroy_deallocate(); + } + ptr_ = std::exchange(other.ptr_, nullptr); + if constexpr (pocma) { + alloc_ = other.alloc_; + } + return *this; + + } else { + if (ptr_) [[likely]] { + this->destroy_deallocate(); + } + if constexpr (pocma) { + ptr_ = other.make_obj(std::move(*other)); + } else { + ptr_ = this->make_obj(std::move(*other)); + } + other.destroy_deallocate(); + return *this; + } + } else [[unlikely]] { // !other.ptr_ + if (ptr_) [[likely]] { + this->destroy_deallocate(); + ptr_ = nullptr; + } + if constexpr (pocma) { + alloc_ = other.alloc_; + } + return *this; + } + } + + template + requires + (!std::is_same_v, indirect_base>) && + std::is_constructible_v && + std::is_assignable_v + constexpr indirect_base& operator=(U&& u) + { + if (ptr_) [[likely]] { + **this = std::forward(u); + + } else [[unlikely]] { + ptr_ = this->make_obj(std::forward(u)); + } + return *this; + } + + [[nodiscard]] constexpr T& operator*() & noexcept IRIS_LIFETIMEBOUND { return *ptr_; } + [[nodiscard]] constexpr T const& operator*() const& noexcept IRIS_LIFETIMEBOUND { return *ptr_; } + [[nodiscard]] constexpr T&& operator*() && noexcept IRIS_LIFETIMEBOUND { return std::move(*ptr_); } + [[nodiscard]] constexpr T const&& operator*() const&& noexcept IRIS_LIFETIMEBOUND { return std::move(*ptr_); } + + [[nodiscard]] constexpr pointer operator->() noexcept IRIS_LIFETIMEBOUND { return ptr_; } + [[nodiscard]] constexpr const_pointer operator->() const noexcept IRIS_LIFETIMEBOUND { return ptr_; } + + [[nodiscard]] constexpr bool valueless_after_move() const noexcept { return ptr_ == nullptr; } + + [[nodiscard]] constexpr allocator_type get_allocator() const noexcept { return alloc_; } + + constexpr void swap(indirect_base& other) + noexcept( + std::allocator_traits::propagate_on_container_swap::value || + std::allocator_traits::is_always_equal::value + ) + { + using std::swap; + swap(ptr_, other.ptr_); + + if constexpr (std::allocator_traits::propagate_on_container_swap::value) { + swap(alloc_, other.alloc_); + } + } + + friend constexpr void swap(indirect_base& lhs, indirect_base& rhs) noexcept(noexcept(lhs.swap(rhs))) + { + return lhs.swap(rhs); + } + +private: + constexpr void destroy_deallocate() + { + assert(ptr_); + std::allocator_traits::destroy(alloc_, std::to_address(ptr_)); + std::allocator_traits::deallocate(alloc_, ptr_, 1); + } + + template + [[nodiscard]] constexpr T* make_obj(Args&&... args) const + { + detail::scoped_allocation sa(alloc_, std::forward(args)...); + return sa.release(); + } + + IRIS_NO_UNIQUE_ADDRESS Allocator alloc_ = Allocator(); + pointer ptr_; +}; + +} // detail + +} // iris + +#endif diff --git a/include/iris/indirect.hpp b/include/iris/indirect.hpp index 112670bd..f5ae4472 100644 --- a/include/iris/indirect.hpp +++ b/include/iris/indirect.hpp @@ -5,7 +5,7 @@ #include // IWYU pragma: keep -#include +#include #include #include @@ -14,357 +14,8 @@ #include #include -#include - namespace iris { -namespace detail { - -template -class scoped_allocation -{ -public: - using pointer = std::allocator_traits::pointer; - using const_pointer = std::allocator_traits::const_pointer; - - template - constexpr explicit scoped_allocation(Alloc const& a, Args&&... args) - : alloc_(a) - , ptr_(std::allocator_traits::allocate(alloc_, 1)) - { - std::allocator_traits::construct(alloc_, this->get(), std::forward(args)...); - } - - constexpr ~scoped_allocation() - noexcept(noexcept(std::allocator_traits::deallocate(alloc_, ptr_, 1))) - { - if (ptr_) [[unlikely]] { - std::allocator_traits::deallocate(alloc_, ptr_, 1); - } - } - - [[nodiscard]] constexpr Alloc get_allocator() const noexcept { return alloc_; } - [[nodiscard]] constexpr auto get() const noexcept { return std::to_address(ptr_); } - [[nodiscard]] constexpr pointer release() noexcept { return std::exchange(ptr_, nullptr); } - -private: - IRIS_NO_UNIQUE_ADDRESS Alloc alloc_; - pointer ptr_; -}; - -// Polyfill for the C++26 `std::indirect` -// https://eel.is/c++draft/indirect -template> -class indirect_base -{ - static_assert(std::is_object_v); - static_assert(!std::is_array_v); - static_assert(!std::is_same_v); - static_assert(!is_ttp_specialization_of_v); - static_assert(!std::is_const_v && !std::is_volatile_v); - static_assert(std::is_same_v::value_type>); - -public: - using value_type = T; - using allocator_type = Allocator; - using pointer = std::allocator_traits::pointer; - using const_pointer = std::allocator_traits::const_pointer; - - constexpr explicit indirect_base() requires std::is_default_constructible_v - : ptr_(this->make_obj()) - {} - - constexpr indirect_base(indirect_base const& other) - : indirect_base( - std::allocator_arg, - std::allocator_traits::select_on_container_copy_construction(other.alloc_), - other - ) - {} - - constexpr explicit indirect_base(std::allocator_arg_t, Allocator const& a) - : alloc_(a) - , ptr_(this->make_obj()) - {} - - constexpr indirect_base(std::allocator_arg_t, Allocator const& a, indirect_base const& other) - : alloc_(a) - , ptr_(other.ptr_ ? this->make_obj(std::as_const(*other.ptr_)) : nullptr) - {} - - constexpr indirect_base(indirect_base&& other) noexcept - : alloc_(std::move(other.alloc_)) - , ptr_(std::exchange(other.ptr_, nullptr)) - {} - - constexpr indirect_base(std::allocator_arg_t, Allocator const& a, indirect_base&& other) - noexcept(std::allocator_traits::is_always_equal::value) - : alloc_(a) - , ptr_(alloc_ == other.alloc_ - ? std::exchange(other.ptr_, nullptr) - : this->make_obj(*std::move(other)) - ) - {} - - template - requires - (!std::is_same_v, indirect_base>) && - (!std::is_same_v, std::in_place_t>) && - std::is_constructible_v && - std::is_default_constructible_v - constexpr explicit indirect_base(U&& u) - : ptr_(this->make_obj(std::forward(u))) - {} - - template - requires - (!std::is_same_v, indirect_base>) && - (!std::is_same_v, std::in_place_t>) && - std::is_constructible_v - constexpr explicit indirect_base(std::allocator_arg_t, Allocator const& a, U&& u) - : alloc_(a) - , ptr_(this->make_obj(std::forward(u))) - {} - - template - requires - std::is_constructible_v && - std::is_default_constructible_v - constexpr explicit indirect_base(std::in_place_t, Us&&... us) - : ptr_(this->make_obj(std::forward(us)...)) - {} - - template - requires - std::is_constructible_v - constexpr explicit indirect_base(std::allocator_arg_t, Allocator const& a, std::in_place_t, Us&&... us) - : alloc_(a) - , ptr_(this->make_obj(std::forward(us)...)) - {} - - template - requires - std::is_constructible_v&, Us...> && - std::is_default_constructible_v - constexpr explicit indirect_base(std::in_place_t, std::initializer_list il, Us&&... us) - : ptr_(this->make_obj(il, std::forward(us)...)) - {} - - template - requires - std::is_constructible_v&, Us...> - constexpr explicit indirect_base(std::allocator_arg_t, Allocator const& a, std::in_place_t, std::initializer_list il, Us&&... us) - : alloc_(a) - , ptr_(this->make_obj(il, std::forward(us)...)) - {} - - constexpr ~indirect_base() noexcept - { - if (ptr_) [[likely]] { - this->destroy_deallocate(); - } - } - - constexpr indirect_base& operator=(indirect_base const& other) - { - if (std::addressof(other) == this) [[unlikely]] { - return *this; - } - - constexpr bool pocca = std::allocator_traits::propagate_on_container_copy_assignment::value; - - if (other.ptr_) [[likely]] { - if constexpr (std::allocator_traits::is_always_equal::value) { // NOLINT(bugprone-branch-clone) - if (ptr_) [[likely]] { - // both contain value and allocator is equal; copy assign - **this = *other; - if constexpr (pocca) { - alloc_ = other.alloc_; - } - return *this; - } - - assert(other.ptr_ && !ptr_); - if constexpr (pocca) { - ptr_ = other.make_obj(*other); - alloc_ = other.alloc_; - } else { - ptr_ = this->make_obj(*other); - } - return *this; - - } else if (alloc_ == other.alloc_) { - if (ptr_) [[likely]] { - // both contain value and allocator is equal; copy assign - **this = *other; - if constexpr (pocca) { - alloc_ = other.alloc_; - } - return *this; - } - - assert(other.ptr_ && !ptr_); - if constexpr (pocca) { - ptr_ = other.make_obj(*other); - alloc_ = other.alloc_; - } else { - ptr_ = this->make_obj(*other); - } - return *this; - - } else { - // other.ptr_ && (ptr_ || !ptr_) - // either allocator is not equal or `this` does not contain value; create copy from `other` - if (ptr_) [[likely]] { - this->destroy_deallocate(); - ptr_ = nullptr; // make it safer - } - if constexpr (pocca) { - ptr_ = other.make_obj(*other); - alloc_ = other.alloc_; - } else { - ptr_ = this->make_obj(*other); - } - return *this; - } - - } else [[unlikely]] { // !other.ptr_ - if (ptr_) [[likely]] { - this->destroy_deallocate(); - ptr_ = nullptr; // make it safer - } - if constexpr (pocca) { - alloc_ = other.alloc_; - } - return *this; - } - } - - constexpr indirect_base& operator=(indirect_base&& other) - noexcept( - std::allocator_traits::propagate_on_container_move_assignment::value || - std::allocator_traits::is_always_equal::value - ) - { - if (std::addressof(other) == this) [[unlikely]] { - return *this; - } - - constexpr bool pocma = std::allocator_traits::propagate_on_container_move_assignment::value; - - if (other.ptr_) [[likely]] { - if constexpr (std::allocator_traits::is_always_equal::value) { - if (ptr_) [[likely]] { - this->destroy_deallocate(); - } - ptr_ = std::exchange(other.ptr_, nullptr); - if constexpr (pocma) { - alloc_ = other.alloc_; - } - return *this; - - } else if (alloc_ == other.alloc_) { - if (ptr_) [[likely]] { - this->destroy_deallocate(); - } - ptr_ = std::exchange(other.ptr_, nullptr); - if constexpr (pocma) { - alloc_ = other.alloc_; - } - return *this; - - } else { - if (ptr_) [[likely]] { - this->destroy_deallocate(); - } - if constexpr (pocma) { - ptr_ = other.make_obj(std::move(*other)); - } else { - ptr_ = this->make_obj(std::move(*other)); - } - other.destroy_deallocate(); - return *this; - } - } else [[unlikely]] { // !other.ptr_ - if (ptr_) [[likely]] { - this->destroy_deallocate(); - ptr_ = nullptr; - } - if constexpr (pocma) { - alloc_ = other.alloc_; - } - return *this; - } - } - - template - requires - (!std::is_same_v, indirect_base>) && - std::is_constructible_v && - std::is_assignable_v - constexpr indirect_base& operator=(U&& u) - { - if (ptr_) [[likely]] { - **this = std::forward(u); - - } else [[unlikely]] { - ptr_ = this->make_obj(std::forward(u)); - } - return *this; - } - - [[nodiscard]] constexpr T& operator*() & noexcept IRIS_LIFETIMEBOUND { return *ptr_; } - [[nodiscard]] constexpr T const& operator*() const& noexcept IRIS_LIFETIMEBOUND { return *ptr_; } - [[nodiscard]] constexpr T&& operator*() && noexcept IRIS_LIFETIMEBOUND { return std::move(*ptr_); } - [[nodiscard]] constexpr T const&& operator*() const&& noexcept IRIS_LIFETIMEBOUND { return std::move(*ptr_); } - - [[nodiscard]] constexpr pointer operator->() noexcept IRIS_LIFETIMEBOUND { return ptr_; } - [[nodiscard]] constexpr const_pointer operator->() const noexcept IRIS_LIFETIMEBOUND { return ptr_; } - - [[nodiscard]] constexpr bool valueless_after_move() const noexcept { return ptr_ == nullptr; } - - [[nodiscard]] constexpr allocator_type get_allocator() const noexcept { return alloc_; } - - constexpr void swap(indirect_base& other) - noexcept( - std::allocator_traits::propagate_on_container_swap::value || - std::allocator_traits::is_always_equal::value - ) - { - using std::swap; - swap(ptr_, other.ptr_); - - if constexpr (std::allocator_traits::propagate_on_container_swap::value) { - swap(alloc_, other.alloc_); - } - } - - friend constexpr void swap(indirect_base& lhs, indirect_base& rhs) noexcept(noexcept(lhs.swap(rhs))) - { - return lhs.swap(rhs); - } - -private: - constexpr void destroy_deallocate() - { - assert(ptr_); - std::allocator_traits::destroy(alloc_, std::to_address(ptr_)); - std::allocator_traits::deallocate(alloc_, ptr_, 1); - } - - template - [[nodiscard]] constexpr T* make_obj(Args&&... args) const - { - detail::scoped_allocation sa(alloc_, std::forward(args)...); - return sa.release(); - } - - IRIS_NO_UNIQUE_ADDRESS Allocator alloc_ = Allocator(); - pointer ptr_; -}; - -} // detail - // Polyfill for the C++26 `std::indirect` // https://eel.is/c++draft/indirect template> diff --git a/include/iris/rvariant/recursive_wrapper.hpp b/include/iris/rvariant/recursive_wrapper.hpp index 1c39ac9e..409beb3f 100644 --- a/include/iris/rvariant/recursive_wrapper.hpp +++ b/include/iris/rvariant/recursive_wrapper.hpp @@ -5,15 +5,17 @@ #include // IWYU pragma: keep +#include #include #include -#include +#include #include #include #include #include +#include namespace iris { @@ -426,14 +428,9 @@ template } // iris -namespace std { - -// --------------------------------------------------- -// recursive_wrapper - template requires ::iris::is_hash_enabled_v -struct hash<::iris::recursive_wrapper> // NOLINT(cert-dcl58-cpp) +struct std::hash<::iris::recursive_wrapper> // NOLINT(cert-dcl58-cpp) { [[nodiscard]] static size_t operator()(::iris::recursive_wrapper const& obj) noexcept(::iris::is_nothrow_hashable_v) @@ -446,12 +443,9 @@ struct hash<::iris::recursive_wrapper> // NOLINT(cert-dcl58-cpp) } }; -// --------------------------------------------------- -// recursive_wrapper_alloca - template requires ::iris::is_hash_enabled_v -struct hash<::iris::recursive_wrapper_alloca> // NOLINT(cert-dcl58-cpp) +struct std::hash<::iris::recursive_wrapper_alloca> // NOLINT(cert-dcl58-cpp) { [[nodiscard]] static size_t operator()(::iris::recursive_wrapper_alloca const& obj) noexcept(::iris::is_nothrow_hashable_v) @@ -464,33 +458,4 @@ struct hash<::iris::recursive_wrapper_alloca> // NOLINT(cert-dcl5 } }; -} // std - - -namespace iris { - -// --------------------------------------------------- -// recursive_wrapper - -template - requires is_hash_enabled_v -[[nodiscard]] std::size_t hash_value(recursive_wrapper const& obj) - noexcept(is_nothrow_hashable_v) -{ - return std::hash>{}(obj); -} - -// --------------------------------------------------- -// recursive_wrapper_alloca - -template - requires is_hash_enabled_v -[[nodiscard]] std::size_t hash_value(recursive_wrapper_alloca const& obj) - noexcept(is_nothrow_hashable_v) -{ - return std::hash>{}(obj); -} - -} // iris - #endif