From 5642cba31b5f7610eddf552b1acd984cf718340d Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Fri, 27 Dec 2024 19:19:40 +0100 Subject: Rework some metaprogramming stuff --- asl/option.hpp | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) (limited to 'asl/option.hpp') diff --git a/asl/option.hpp b/asl/option.hpp index b22b295..8deed69 100644 --- a/asl/option.hpp +++ b/asl/option.hpp @@ -63,23 +63,15 @@ concept is_option = requires template class option { - static constexpr bool kIsTrivial = - trivially_default_constructible && - trivially_copy_constructible && - trivially_move_constructible && - trivially_copy_assignable && - trivially_move_assignable && - trivially_destructible; - static constexpr bool kHasNiche = has_niche; - static constexpr bool kHasInlinePayload = kIsTrivial || kHasNiche; + static constexpr bool kHasInlinePayload = default_constructible || kHasNiche; using Storage = select_t>; using HasValueMarker = select_t; - Storage m_payload{}; - ASL_NO_UNIQUE_ADDRESS HasValueMarker m_has_value; + Storage m_payload; + ASL_NO_UNIQUE_ADDRESS HasValueMarker m_has_value{}; template constexpr void construct(Args&&... args) @@ -88,7 +80,7 @@ class option if constexpr (kHasInlinePayload) { - construct_at(&m_payload, ASL_FWD(args)...); + m_payload = T(ASL_FWD(args)...); } else { @@ -122,9 +114,11 @@ public: constexpr option() : option(nullopt) {} // NOLINTNEXTLINE(*-explicit-conversions) - constexpr option(nullopt_t) requires (!kHasNiche) + constexpr option(nullopt_t) requires (!kHasNiche) && trivially_default_constructible {} + + // NOLINTNEXTLINE(*-explicit-conversions) + constexpr option(nullopt_t) requires (!kHasNiche) && (!trivially_default_constructible) : m_payload{} - , m_has_value{false} {} // NOLINTNEXTLINE(*-explicit-conversions) @@ -167,7 +161,7 @@ public: !same_as, option> && !is_niche ) - : option() + : option(nullopt) { construct(ASL_FWD(value)); } @@ -177,7 +171,7 @@ public: constexpr option(const option& other) requires copy_constructible && (!kHasInlinePayload) - : option() + : option(nullopt) { if (other.has_value()) { @@ -193,7 +187,7 @@ public: constexpr option(option&& other) requires move_constructible && (!kHasInlinePayload) - : option() + : option(nullopt) { if (other.has_value()) { @@ -211,7 +205,7 @@ public: constructible_from && !option_internal::convertible_constructible_from_option ) - : option() + : option(nullopt) { if (other.has_value()) { @@ -226,7 +220,7 @@ public: constructible_from && !option_internal::convertible_constructible_from_option ) - : option() + : option(nullopt) { if (other.has_value()) { @@ -415,6 +409,7 @@ public: } } + // @Todo(C++23) Deducing this constexpr T&& value() && { ASL_ASSERT_RELEASE(has_value()); -- cgit