37#include <system_error>
45 : std::runtime_error(code.message())
50#if __cpp_exceptions < 199711
51constexpr static bool clev_exception_disabled_v =
true;
53constexpr static bool clev_exception_disabled_v =
false;
57abort_now(
const std::error_code err)
noexcept(clev_exception_disabled_v)
59 if constexpr (clev_exception_disabled_v)
60 std::quick_exit(err.value());
65inline static void exit_now(
const std::error_code err)
noexcept
67 std::quick_exit(
err.value());
73 using parent = std::unexpected<std::error_code>;
79template<
typename value_t>
87template<
template<
typename>
typename expected_t,
typename value_t>
89 : std::is_same<expected_t<value_t>, expected<value_t>>
93template<
typename expected_t>
98struct expected<void> :
public std::expected<void, std::error_code>
101 using parent = std::expected<void, std::error_code>;
104 using parent::parent;
106 constexpr void or_abort() noexcept(clev_exception_disabled_v)
109 abort_now(expected::error());
115 exit_now(expected::error());
118 template<
typename to_value_t>
128template<
typename value_t>
129struct expected :
public std::expected<value_t, std::error_code>
132 using parent = std::expected<value_t, std::error_code>;
134 using value_type = parent::value_type;
136 using error_type = parent::error_type;
139 using parent::parent;
141 [[nodiscard]] expected::value_type&
145 abort_now(expected::error());
147 return expected::value();
150 [[nodiscard]] expected::value_type&&
154 abort_now(expected::error());
156 return std::move(expected::value());
159 [[nodiscard]]
constexpr const expected::value_type&
163 abort_now(expected::error());
165 return expected::value();
168 [[nodiscard]]
constexpr const expected::value_type&&
172 abort_now(expected::error());
174 return std::move(expected::value());
182 dest = std::move(expected::value());
184 return std::ref(dest);
187 return unexpected{std::move(expected::error())};
190 template<
typename fn_t>
191 requires std::is_constructible_v<error_type, error_type&>
194 using exp_t = std::remove_cvref_t<
195 std::invoke_result_t<fn_t,
decltype(std::move(expected::value()))>>;
198 "expected<value_t>::and_then(fn_t) must specialize");
200 static_assert(std::is_same_v<typename exp_t::error_type, error_type>,
201 "expected<value_t>::and_then(fn_t) requires same error");
204 return std::invoke(std::forward<fn_t>(f), std::move(expected::value()));
206 return exp_t{std::unexpect, std::move(expected::error())};
209 template<
typename to_value_t>
218 template<
typename to_value_t>
224 return std::move(*
this);
236template<
typename value_t>
239template<
typename enum_t>
clev::expected< void > make_expected(const int e) noexcept
expected(value_t &&) -> expected< value_t >
constexpr bool is_expected_specialization_v
exception(std::error_code code) noexcept
auto transform_to(to_value_t &&v) &&noexcept
constexpr void or_exit() noexcept
constexpr void or_abort() noexcept(clev_exception_disabled_v)
expected::value_type && value_or_abort() &&noexcept(clev_exception_disabled_v)
auto transform_to() &&noexcept
auto and_then(fn_t &&f) &&noexcept
constexpr const expected::value_type && value_or_abort() const &&noexcept(clev_exception_disabled_v)
constexpr const expected::value_type & value_or_abort() const &noexcept(clev_exception_disabled_v)
expected< std::reference_wrapper< value_t > > and_assign_to(value_t &dest) &&noexcept
expected::value_type & value_or_abort() &noexcept(clev_exception_disabled_v)
auto transform_to(to_value_t &&v) &&noexcept
auto discard_value() &&noexcept