From d638edf012da19b03ebb57858bee54f8719a19b3 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Thu, 1 Feb 2024 18:46:47 -0500 Subject: [PATCH] more std::expected --- include/blt/std/utility.h | 60 +++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 12 deletions(-) diff --git a/include/blt/std/utility.h b/include/blt/std/utility.h index 21a9dd7..8f2cfe8 100644 --- a/include/blt/std/utility.h +++ b/include/blt/std/utility.h @@ -21,6 +21,7 @@ #include #include +#include #if defined(__GNUC__) @@ -214,30 +215,65 @@ namespace blt class expected { private: - template - inline static constexpr bool isCopyMovable() - { - return std::is_move_constructible_v && std::is_copy_constructible_v - } - std::optional t; std::optional e; public: constexpr expected() noexcept: t(T()) {} - constexpr explicit expected(const T& t): t(t) + template, bool> = true> + constexpr explicit expected(U&& t): t(std::forward(t)) {} - constexpr explicit expected(T&& t): t(std::forward(t)) + template, bool> = true> + constexpr explicit expected(U&& e): e(std::forward(e)) {} - constexpr explicit expected(const E& e): e(e) +// template, bool> = true> +// constexpr expected(std::initializer_list t): t(std::move(*t.begin())) +// {} +// +// template, bool> = true> +// constexpr expected(std::initializer_list e): e(std::move(*e.begin())) +// {} + + template && std::is_convertible_v, bool> = true> + constexpr explicit expected(const expected& other) + { + if (other.has_value()) + t = other.value(); + else + e = other.error(); + } + + template && std::is_convertible_v, bool> = true> + constexpr explicit expected(expected&& other) + { + if (other.has_value()) + t = other.value(); + else + e = other.error(); + } + + constexpr expected(const T& t): t(t) {} - constexpr explicit expected(E&& e): e(std::forward(e)) + constexpr expected(T&& t): t(std::move(t)) {} + constexpr expected(const E& e): e(e) + {} + + constexpr expected(E&& e): e(std::move(e)) + {} + + constexpr expected(const expected& copy) = default; + + constexpr expected(expected&& move) = default; + + expected& operator=(const expected& copy) = default; + + expected& operator=(expected&& move) = default; [[nodiscard]] constexpr explicit operator bool() const noexcept { @@ -289,13 +325,13 @@ namespace blt return e.value(); } - template && std::is_copy_constructible_v, int*> = 0> + template && std::is_copy_constructible_v, bool> = true> constexpr T value_or(U&& default_value) const& { return bool(*this) ? **this : static_cast(std::forward(default_value)); } - template && std::is_move_constructible_v, int*> = 0 > + template && std::is_move_constructible_v, bool> = true> constexpr T value_or(U&& default_value)&& { return bool(*this) ? std::move(**this) : static_cast(std::forward(default_value));