From c2387e65643364d33fd5515515da709b4b7b7118 Mon Sep 17 00:00:00 2001 From: Brett Date: Sat, 3 Feb 2024 17:09:59 -0500 Subject: [PATCH] expected seems to be working --- include/blt/std/utility.h | 112 ++++++++++++++++++++++++++++---------- 1 file changed, 82 insertions(+), 30 deletions(-) diff --git a/include/blt/std/utility.h b/include/blt/std/utility.h index eb4b72a..e8f8363 100644 --- a/include/blt/std/utility.h +++ b/include/blt/std/utility.h @@ -235,7 +235,7 @@ namespace blt template, unexpected> && !std::is_same_v, std::in_place_t> && std::is_constructible_v, bool> = true> - constexpr explicit unexpected(Err&& e): e(std::forward(e)) + constexpr explicit unexpected(Err&& e): e(std::forward(e)) {} template, bool> = true> @@ -315,6 +315,19 @@ namespace blt { protected: std::variant v; + + template + inline static constexpr bool eight_insanity_v = + std::is_constructible_v&> || std::is_constructible_v> || + std::is_constructible_v&> || std::is_constructible_v> || + std::is_convertible_v&, T> || std::is_convertible_v, T> || + std::is_convertible_v&, T> || std::is_convertible_v, T>; + + template + inline static constexpr bool four_insanity_v = + std::is_constructible_v, expected&> || std::is_constructible_v, expected> || + std::is_constructible_v, const expected&> || std::is_constructible_v, const expected>; + public: template, bool> = true> constexpr expected() noexcept: v(T()) @@ -322,19 +335,42 @@ namespace blt constexpr expected(const expected& copy) = delete; - constexpr expected(expected&& move) noexcept : v(move ? std::move(*move) : std::move(move.error())) + constexpr expected(expected&& move) noexcept: v(move ? std::move(*move) : std::move(move.error())) {} /* * (4)...(5) */ + template, class GF = const G&, std::enable_if_t< + (!std::is_convertible_v || !std::is_convertible_v) && (std::is_constructible_v || std::is_void_v) && + std::is_constructible_v && !eight_insanity_v < U, G>&& !four_insanity_v, bool> = true> - template - constexpr explicit expected(const expected& other) + constexpr explicit expected(const expected& other): + v(other.has_value() ? std::forward(*other) : std::forward(other.error())) {} - template - constexpr explicit expected(expected&& other) + template || !std::is_convertible_v) && (std::is_constructible_v || std::is_void_v) && + std::is_constructible_v && !eight_insanity_v < U, G>&& !four_insanity_v, bool> = true> + + constexpr explicit expected(expected&& other): + v(other.has_value() ? std::forward(*other) : std::forward(other.error())) + {} + + template, class GF = const G&, std::enable_if_t< + (std::is_convertible_v && std::is_convertible_v) && (std::is_constructible_v || std::is_void_v) && + std::is_constructible_v && !eight_insanity_v < U, G>&& !four_insanity_v, bool> = true> + + constexpr expected(const expected& other): + v(other.has_value() ? std::forward(*other) : std::forward(other.error())) + {} + + template && std::is_convertible_v) && (std::is_constructible_v || std::is_void_v) && + std::is_constructible_v && !eight_insanity_v < U, G>&& !four_insanity_v, bool> = true> + + constexpr expected(expected&& other): + v(other.has_value() ? std::forward(*other) : std::forward(other.error())) {} @@ -342,59 +378,75 @@ namespace blt * (6) */ - template, bool> = true> - constexpr explicit expected(U&& v) + template && + !std::is_same_v, void> && + !std::is_same_v, std::in_place_t> && + !std::is_same_v> && + std::is_constructible_v && + !std::is_same_v, unexpected> && + !std::is_same_v, expected>, bool> = true> + constexpr explicit expected(U&& v): v(T(std::forward(v))) {} - template, bool> = true> - constexpr expected(U&& v) + template && + !std::is_same_v, void> && + !std::is_same_v, std::in_place_t> && + !std::is_same_v> && + std::is_constructible_v && + !std::is_same_v, unexpected> && + !std::is_same_v, expected>, bool> = true> + constexpr expected(U&& v): v(T(std::forward(v))) {} /* * (7) */ - template, bool> = true> - constexpr explicit expected(const unexpected& e) + template>, std::enable_if_t< + !std::is_convertible_v && std::is_constructible_v, bool> = true> + constexpr explicit expected(const unexpected& e): v(std::forward(e.error())) {} - template, bool> = true> - constexpr expected(const unexpected& e) + template>, std::enable_if_t< + std::is_convertible_v && std::is_constructible_v, bool> = true> + constexpr expected(const unexpected& e): v(std::forward(e.error())) {} /* * (8) */ - template, bool> = true> - constexpr explicit expected(const unexpected& e) + template>, std::enable_if_t< + !std::is_convertible_v && std::is_constructible_v, bool> = true> + constexpr explicit expected(unexpected&& e): v(std::forward(e.error())) {} - template, bool> = true> - constexpr expected(const unexpected& e) + template>, std::enable_if_t< + std::is_convertible_v && std::is_constructible_v, bool> = true> + constexpr expected(unexpected&& e): v(std::forward(e.error())) {} /* * (9)...(13) */ - template - constexpr explicit expected(std::in_place_t, Args&& ... args) + template, bool> = true> + constexpr explicit expected(std::in_place_t, Args&& ... args): v(T(std::forward(args)...)) {} - template - constexpr explicit expected(std::in_place_t, std::initializer_list il, Args&& ... args) + template&, Args...>, bool> = true> + constexpr explicit expected(std::in_place_t, std::initializer_list il, Args&& ... args): v(T(il, std::forward(args)...)) {} - template - constexpr explicit expected(std::in_place_t) noexcept +// template, void>, bool> = true> +// constexpr explicit expected(std::in_place_t) noexcept: v(T()) +// {} + + template, bool> = true> + constexpr explicit expected(unexpect_t, Args&& ... args): v(E(std::forward(args)...)) {} - template - constexpr explicit expected(unexpect_t, Args&& ... args) - {} - - template - constexpr explicit expected(unexpect_t, std::initializer_list il, Args&& ... args) + template&, Args...>, bool> = true> + constexpr explicit expected(unexpect_t, std::initializer_list il, Args&& ... args): v(E(il, std::forward(args)...)) {} expected& operator=(const expected& copy) = delete;