8#ifndef DESIGNLAB_MY_EXPECTED_H_
9#define DESIGNLAB_MY_EXPECTED_H_
29 const char*
what() const noexcept
override {
return message_.c_str(); }
35template <
class T,
class W>
37 std::is_constructible<T, W&>, std::is_convertible<W&, T>,
38 std::is_constructible<T, W>, std::is_convertible<W, T>,
39 std::is_constructible<T, const W&>, std::is_convertible<const W&, T>,
40 std::is_constructible<T, const W>, std::is_convertible<const W, T>>;
45template <impl::IsExpected T,
typename E>
55 requires std::is_default_constructible_v<T>
56 : storage_(T{}), has_value_(
true) {}
59 : storage_(rhs.storage_), has_value_(rhs.has_value_) {}
62 requires(std::is_move_constructible_v<T> && std::is_move_constructible_v<E>)
63 : storage_(
std::move(rhs.storage_)), has_value_(rhs.has_value_) {}
65 template <
class U,
class G>
67 requires(std::is_constructible_v<T, const U&> &&
68 std::is_constructible_v<E, const G&> &&
69 !converts_from_any_cvref<T, expected<U, G>&> &&
74 : storage_(rhs.storage_), has_value_(rhs.has_value_) {}
76 template <
class U,
class G>
78 requires(std::is_constructible_v<T, U> && std::is_constructible_v<E, G> &&
79 !converts_from_any_cvref<T, expected<U, G>> &&
84 : storage_(
std::move(rhs.storage_)), has_value_(rhs.has_value_) {}
86 template <
class U = T>
88 requires(std::is_constructible_v<T, U> &&
89 !std::is_same_v<expected, std::remove_cvref_t<U>> &&
90 !std::is_same_v<std::remove_cvref_t<U>, std::in_place_t>)
91 : storage_(v), has_value_(true) {}
102 return std::addressof(std::get<T>(storage_));
108 return std::addressof(std::get<T>(storage_));
114 return std::get<T>(storage_);
119 constexpr T&
operator*() &
noexcept {
return std::get<T>(storage_); }
121 constexpr explicit operator bool() const noexcept {
return has_value_; }
124 constexpr bool has_value() const noexcept {
return has_value_; }
131 return std::get<T>(storage_);
139 return std::get<T>(storage_);
147 auto& unexpected_value = std::get<unexpected<E>>(storage_);
148 return unexpected_value.error();
156 auto& unexpected_value = std::get<unexpected<E>>(storage_);
157 return unexpected_value.error();
162 requires(std::is_constructible_v<T, U> && std::is_copy_constructible_v<T>)
164 return has_value() ? **this :
static_cast<T
>(std::forward<U>(v));
167 template <
class G = E>
169 requires(std::is_constructible_v<E, G> && std::is_copy_constructible_v<E>)
175 template <
class Return>
180 return ReturnType{func(
value())};
186 template <
class Func,
class Ret = std::invoke_result_t<Func, T>>
188 requires(std::is_invocable_r_v<Ret, Func, T> &&
189 std::is_same_v<typename Ret::error_type, E> &&
190 std::is_same_v<std::remove_cvref_t<Ret>,
193 using ReturnType = std::invoke_result_t<Func, T>;
195 return ReturnType{func(
value())};
202 std::variant<T, unexpected<E>> storage_;
bad_expected_access(const char *msg)
bad_expected_access(const std::string &msg)
const char * what() const noexcept override
constexpr expected(const expected< U, G > &rhs)
constexpr bool has_value() const noexcept
正常値を持っている場合に true を返す.
constexpr E & error()
失敗時の値を返す.
constexpr const E & error() const
失敗時の値を返す.
constexpr const T & value() const
正常値を返す.
constexpr auto and_then(Func &&func) const &
constexpr expected(expected &&rhs) noexcept
constexpr expected(const expected< U, G > &&rhs) noexcept
constexpr expected(U &&v)
constexpr const T * operator->() const noexcept
正常値のポインタを返す.
constexpr expected(const unexpected< G > &e)
constexpr T & operator*() &noexcept
正常値を参照する.
constexpr expected(const expected &rhs)
constexpr expected< Return, E > and_then(const std::function< expected< Return, E >(T)> &func) const &
constexpr expected()
デフォルトコンストラクタ
constexpr const T & operator*() const &noexcept
正常値を参照する.
constexpr T value_or(U &&v) const &
constexpr E error_or(G &&e) const &
constexpr T * operator->() noexcept
正常値のポインタを返す.
constexpr T & value()
正常値を返す.
constexpr ~expected()=default
デストラクタ
constexpr bool converts_from_any_cvref