mirror of
https://github.com/TartanLlama/expected.git
synced 2025-08-05 11:54:30 +02:00
More comparisons
This commit is contained in:
721
expected.hpp
721
expected.hpp
@@ -16,9 +16,9 @@
|
|||||||
|
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <iostream>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
#if (defined(_MSC_VER) && _MSC_VER == 1900)
|
#if (defined(_MSC_VER) && _MSC_VER == 1900)
|
||||||
#define TL_EXPECTED_MSVC2015
|
#define TL_EXPECTED_MSVC2015
|
||||||
@@ -163,9 +163,8 @@ constexpr bool operator>=(const unexpected<E> &lhs, const unexpected<E> &rhs) {
|
|||||||
return lhs.value() >= rhs.value();
|
return lhs.value() >= rhs.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class E>
|
template <class E> unexpected<E> make_unexpected(E &&e) {
|
||||||
unexpected<E> make_unexpected (E&& e) {
|
return unexpected<E>(std::forward<E>(e));
|
||||||
return unexpected<E>(std::forward<E>(e));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct unexpect_t {
|
struct unexpect_t {
|
||||||
@@ -398,51 +397,52 @@ template <class E> struct expected_storage_base<void, E, false, false> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// TODO, conditionally delete things
|
// TODO, conditionally delete things
|
||||||
template <class T, class E> struct expected_ctor_base {};
|
template <class T, class E> struct expected_ctor_base {};
|
||||||
template <class T, class E,
|
template <class T, class E,
|
||||||
bool = (std::is_copy_assignable<T>::value && std::is_copy_assignable<E>::value &&
|
bool = (std::is_copy_assignable<T>::value &&
|
||||||
std::is_copy_constructible<E>::value && std::is_copy_constructible<T>::value &&
|
std::is_copy_assignable<E>::value &&
|
||||||
std::is_nothrow_move_constructible<E>::value),
|
std::is_copy_constructible<E>::value &&
|
||||||
bool = (std::is_move_constructible<T>::value && std::is_move_assignable<T>::value &&
|
std::is_copy_constructible<T>::value &&
|
||||||
std::is_nothrow_move_constructible<E>::value && std::is_nothrow_move_assignable<E>::value)>
|
std::is_nothrow_move_constructible<E>::value),
|
||||||
struct expected_assign_base {
|
bool = (std::is_move_constructible<T>::value &&
|
||||||
expected_assign_base() = default;
|
std::is_move_assignable<T>::value &&
|
||||||
~expected_assign_base() = default;
|
std::is_nothrow_move_constructible<E>::value &&
|
||||||
expected_assign_base(const expected_assign_base&) = default;
|
std::is_nothrow_move_assignable<E>::value)>
|
||||||
expected_assign_base(expected_assign_base&&) noexcept = default;
|
struct expected_assign_base {
|
||||||
expected_assign_base& operator=(const expected_assign_base&) = default;
|
expected_assign_base() = default;
|
||||||
expected_assign_base& operator=(expected_assign_base&&) noexcept = default;
|
~expected_assign_base() = default;
|
||||||
};
|
expected_assign_base(const expected_assign_base &) = default;
|
||||||
|
expected_assign_base(expected_assign_base &&) noexcept = default;
|
||||||
|
expected_assign_base &operator=(const expected_assign_base &) = default;
|
||||||
|
expected_assign_base &operator=(expected_assign_base &&) noexcept = default;
|
||||||
|
};
|
||||||
|
|
||||||
template <class T, class E>
|
template <class T, class E> struct expected_assign_base<T, E, true, false> {
|
||||||
struct expected_assign_base<T,E,true,false> {
|
expected_assign_base() = default;
|
||||||
expected_assign_base() = default;
|
~expected_assign_base() = default;
|
||||||
~expected_assign_base() = default;
|
expected_assign_base(const expected_assign_base &) = default;
|
||||||
expected_assign_base(const expected_assign_base&) = default;
|
expected_assign_base(expected_assign_base &&) noexcept = default;
|
||||||
expected_assign_base(expected_assign_base&&) noexcept = default;
|
expected_assign_base &operator=(const expected_assign_base &) = default;
|
||||||
expected_assign_base& operator=(const expected_assign_base&) = default;
|
expected_assign_base &operator=(expected_assign_base &&) noexcept = delete;
|
||||||
expected_assign_base& operator=(expected_assign_base&&) noexcept = delete;
|
};
|
||||||
};
|
|
||||||
|
|
||||||
template <class T, class E>
|
template <class T, class E> struct expected_assign_base<T, E, false, true> {
|
||||||
struct expected_assign_base<T,E,false,true> {
|
expected_assign_base() = default;
|
||||||
expected_assign_base() = default;
|
~expected_assign_base() = default;
|
||||||
~expected_assign_base() = default;
|
expected_assign_base(const expected_assign_base &) = default;
|
||||||
expected_assign_base(const expected_assign_base&) = default;
|
expected_assign_base(expected_assign_base &&) noexcept = default;
|
||||||
expected_assign_base(expected_assign_base&&) noexcept = default;
|
expected_assign_base &operator=(const expected_assign_base &) = delete;
|
||||||
expected_assign_base& operator=(const expected_assign_base&) = delete;
|
expected_assign_base &operator=(expected_assign_base &&) noexcept = default;
|
||||||
expected_assign_base& operator=(expected_assign_base&&) noexcept = default;
|
};
|
||||||
};
|
|
||||||
|
|
||||||
template <class T, class E>
|
template <class T, class E> struct expected_assign_base<T, E, false, false> {
|
||||||
struct expected_assign_base<T,E,false,false> {
|
expected_assign_base() = default;
|
||||||
expected_assign_base() = default;
|
~expected_assign_base() = default;
|
||||||
~expected_assign_base() = default;
|
expected_assign_base(const expected_assign_base &) = default;
|
||||||
expected_assign_base(const expected_assign_base&) = default;
|
expected_assign_base(expected_assign_base &&) noexcept = default;
|
||||||
expected_assign_base(expected_assign_base&&) noexcept = default;
|
expected_assign_base &operator=(const expected_assign_base &) = delete;
|
||||||
expected_assign_base& operator=(const expected_assign_base&) = delete;
|
expected_assign_base &operator=(expected_assign_base &&) noexcept = delete;
|
||||||
expected_assign_base& operator=(expected_assign_base&&) noexcept = delete;
|
};
|
||||||
};
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
template <class E> class bad_expected_access : public std::exception {
|
template <class E> class bad_expected_access : public std::exception {
|
||||||
@@ -464,8 +464,7 @@ private:
|
|||||||
|
|
||||||
template <class T, class E>
|
template <class T, class E>
|
||||||
class expected : private detail::expected_storage_base<T, E>,
|
class expected : private detail::expected_storage_base<T, E>,
|
||||||
private detail::expected_assign_base<T, E>
|
private detail::expected_assign_base<T, E> {
|
||||||
{
|
|
||||||
static_assert(!std::is_reference<T>::value, "T must not be a reference");
|
static_assert(!std::is_reference<T>::value, "T must not be a reference");
|
||||||
static_assert(!std::is_same<T, std::remove_cv<in_place_t>>::value,
|
static_assert(!std::is_same<T, std::remove_cv<in_place_t>>::value,
|
||||||
"T must not be in_place_t");
|
"T must not be in_place_t");
|
||||||
@@ -490,7 +489,8 @@ public:
|
|||||||
typedef E error_type;
|
typedef E error_type;
|
||||||
typedef unexpected<E> unexpected_type;
|
typedef unexpected<E> unexpected_type;
|
||||||
|
|
||||||
#if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && !defined(TL_EXPECTED_GCC54)
|
#if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \
|
||||||
|
!defined(TL_EXPECTED_GCC54)
|
||||||
/// \group and_then
|
/// \group and_then
|
||||||
/// Carries out some operation which returns an optional on the stored object
|
/// Carries out some operation which returns an optional on the stored object
|
||||||
/// if there is one. \requires `std::invoke(std::forward<F>(f), value())`
|
/// if there is one. \requires `std::invoke(std::forward<F>(f), value())`
|
||||||
@@ -604,7 +604,8 @@ public:
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && !defined(TL_EXPECTED_GCC54)
|
#if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \
|
||||||
|
!defined(TL_EXPECTED_GCC54)
|
||||||
/// \brief Carries out some operation on the stored object if there is one.
|
/// \brief Carries out some operation on the stored object if there is one.
|
||||||
/// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
|
/// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
|
||||||
/// value())`. Returns a `std::expected<U>`. The return value is empty if
|
/// value())`. Returns a `std::expected<U>`. The return value is empty if
|
||||||
@@ -664,7 +665,8 @@ public:
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && !defined(TL_EXPECTED_GCC54)
|
#if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \
|
||||||
|
!defined(TL_EXPECTED_GCC54)
|
||||||
/// \brief Carries out some operation on the stored object if there is one.
|
/// \brief Carries out some operation on the stored object if there is one.
|
||||||
/// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
|
/// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
|
||||||
/// value())`. Returns a `std::expected<U>`. The return value is empty if
|
/// value())`. Returns a `std::expected<U>`. The return value is empty if
|
||||||
@@ -753,7 +755,8 @@ public:
|
|||||||
detail::enable_if_t<std::is_constructible<E, const G &>::value> * =
|
detail::enable_if_t<std::is_constructible<E, const G &>::value> * =
|
||||||
nullptr,
|
nullptr,
|
||||||
detail::enable_if_t<std::is_convertible<const G &, E>::value> * = nullptr>
|
detail::enable_if_t<std::is_convertible<const G &, E>::value> * = nullptr>
|
||||||
constexpr expected(unexpected<G> const &e) : storage_base(unexpect, e.value()) {}
|
constexpr expected(unexpected<G> const &e)
|
||||||
|
: storage_base(unexpect, e.value()) {}
|
||||||
|
|
||||||
template <
|
template <
|
||||||
class G = E,
|
class G = E,
|
||||||
@@ -853,163 +856,157 @@ public:
|
|||||||
constexpr expected(U &&v) : expected(in_place, std::forward<U>(v)) {}
|
constexpr expected(U &&v) : expected(in_place, std::forward<U>(v)) {}
|
||||||
|
|
||||||
// TODO conditionally delete
|
// TODO conditionally delete
|
||||||
expected& operator=(const expected& rhs) {
|
expected &operator=(const expected &rhs) { return assign(rhs); }
|
||||||
return assign(rhs);
|
|
||||||
|
expected &operator=(expected &&rhs) { return assign(std::move(rhs)); }
|
||||||
|
|
||||||
|
template <
|
||||||
|
class U = T,
|
||||||
|
detail::enable_if_t<
|
||||||
|
(!std::is_same<expected<T, E>, detail::decay_t<U>>::value &&
|
||||||
|
!detail::conjunction<std::is_scalar<T>,
|
||||||
|
std::is_same<T, detail::decay_t<U>>>::value &&
|
||||||
|
std::is_constructible<T, U>::value &&
|
||||||
|
std::is_assignable<T &, U>::value &&
|
||||||
|
std::is_nothrow_move_constructible<E>::value)> * = nullptr,
|
||||||
|
detail::enable_if_t<std::is_nothrow_constructible<T, U &&>::value> * =
|
||||||
|
nullptr>
|
||||||
|
expected &operator=(U &&v) {
|
||||||
|
if (has_value()) {
|
||||||
|
val() = std::forward<U>(v);
|
||||||
|
} else {
|
||||||
|
err().~unexpected<E>();
|
||||||
|
::new (valptr()) T(std::forward<U>(v));
|
||||||
|
this->m_has_val = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
expected& operator=(expected&& rhs) {
|
return *this;
|
||||||
return assign(std::move(rhs));
|
}
|
||||||
|
|
||||||
|
template <
|
||||||
|
class U = T,
|
||||||
|
detail::enable_if_t<
|
||||||
|
(!std::is_same<expected<T, E>, detail::decay_t<U>>::value &&
|
||||||
|
!detail::conjunction<std::is_scalar<T>,
|
||||||
|
std::is_same<T, detail::decay_t<U>>>::value &&
|
||||||
|
std::is_constructible<T, U>::value &&
|
||||||
|
std::is_assignable<T &, U>::value &&
|
||||||
|
std::is_nothrow_move_constructible<E>::value)> * = nullptr,
|
||||||
|
detail::enable_if_t<!std::is_nothrow_constructible<T, U &&>::value> * =
|
||||||
|
nullptr>
|
||||||
|
expected &operator=(U &&v) {
|
||||||
|
if (has_value()) {
|
||||||
|
val() = std::forward<U>(v);
|
||||||
|
} else {
|
||||||
|
auto tmp = std::move(err());
|
||||||
|
err().~unexpected<E>();
|
||||||
|
try {
|
||||||
|
::new (valptr()) T(std::move(v));
|
||||||
|
this->m_has_val = true;
|
||||||
|
} catch (...) {
|
||||||
|
err() = std::move(tmp);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class U = T,
|
return *this;
|
||||||
detail::enable_if_t<
|
}
|
||||||
(!std::is_same<expected<T,E>, detail::decay_t<U>>::value &&
|
|
||||||
!detail::conjunction<std::is_scalar<T>, std::is_same<T, detail::decay_t<U>>>::value &&
|
|
||||||
std::is_constructible<T, U>::value &&
|
|
||||||
std::is_assignable<T&, U>::value &&
|
|
||||||
std::is_nothrow_move_constructible<E>::value)>* = nullptr,
|
|
||||||
detail::enable_if_t<std::is_nothrow_constructible<T, U&&>::value>* = nullptr>
|
|
||||||
expected &operator=(U &&v) {
|
|
||||||
if (has_value()) {
|
|
||||||
val() = std::forward<U>(v);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
err().~unexpected<E>();
|
|
||||||
::new (valptr()) T (std::forward<U>(v));
|
|
||||||
this->m_has_val = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return *this;
|
template <class G = E,
|
||||||
|
detail::enable_if_t<std::is_nothrow_copy_constructible<G>::value &&
|
||||||
|
std::is_assignable<G &, G>::value> * = nullptr>
|
||||||
|
expected &operator=(const unexpected<G> &rhs) {
|
||||||
|
if (!has_value()) {
|
||||||
|
err() = rhs;
|
||||||
|
} else {
|
||||||
|
val().~T();
|
||||||
|
::new (errptr()) unexpected<E>(rhs);
|
||||||
|
this->m_has_val = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class U = T,
|
return *this;
|
||||||
detail::enable_if_t<
|
}
|
||||||
(!std::is_same<expected<T,E>, detail::decay_t<U>>::value &&
|
|
||||||
!detail::conjunction<std::is_scalar<T>, std::is_same<T, detail::decay_t<U>>>::value &&
|
|
||||||
std::is_constructible<T, U>::value &&
|
|
||||||
std::is_assignable<T&, U>::value &&
|
|
||||||
std::is_nothrow_move_constructible<E>::value)>* = nullptr,
|
|
||||||
detail::enable_if_t<!std::is_nothrow_constructible<T, U&&>::value>* = nullptr>
|
|
||||||
expected &operator=(U &&v) {
|
|
||||||
if (has_value()) {
|
|
||||||
val() = std::forward<U>(v);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
auto tmp = std::move(err());
|
|
||||||
err().~unexpected<E>();
|
|
||||||
try {
|
|
||||||
::new (valptr()) T (std::move(v));
|
|
||||||
this->m_has_val = true;
|
|
||||||
}
|
|
||||||
catch (...) {
|
|
||||||
err() = std::move(tmp);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return *this;
|
template <class G = E,
|
||||||
|
detail::enable_if_t<std::is_nothrow_move_constructible<G>::value &&
|
||||||
|
std::is_move_assignable<G>::value> * = nullptr>
|
||||||
|
expected &operator=(unexpected<G> &&rhs) noexcept {
|
||||||
|
if (!has_value()) {
|
||||||
|
err() = std::move(rhs);
|
||||||
|
} else {
|
||||||
|
val().~T();
|
||||||
|
::new (errptr()) unexpected<E>(std::move(rhs));
|
||||||
|
this->m_has_val = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
template <class G = E,
|
template <class... Args, detail::enable_if_t<std::is_nothrow_constructible<
|
||||||
detail::enable_if_t<std::is_nothrow_copy_constructible<G>::value && std::is_assignable<G&, G>::value>* = nullptr>
|
T, Args &&...>::value> * = nullptr>
|
||||||
expected &operator=(const unexpected<G> &rhs) {
|
void emplace(Args &&... args) {
|
||||||
if (!has_value()) {
|
if (has_value()) {
|
||||||
err() = rhs;
|
val() = T(std::forward<Args>(args)...);
|
||||||
}
|
} else {
|
||||||
else {
|
err().~unexpected<E>();
|
||||||
val().~T();
|
::new (valptr()) T(std::forward<Args>(args)...);
|
||||||
::new (errptr()) unexpected<E> (rhs);
|
this->m_has_val = true;
|
||||||
this->m_has_val = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return *this;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template <class G = E,
|
template <class... Args, detail::enable_if_t<!std::is_nothrow_constructible<
|
||||||
detail::enable_if_t<std::is_nothrow_move_constructible<G>::value && std::is_move_assignable<G>::value>* = nullptr>
|
T, Args &&...>::value> * = nullptr>
|
||||||
expected &operator=(unexpected<G> && rhs) noexcept {
|
void emplace(Args &&... args) {
|
||||||
if (!has_value()) {
|
if (has_value()) {
|
||||||
err() = std::move(rhs);
|
val() = T(std::forward<Args>(args)...);
|
||||||
}
|
} else {
|
||||||
else {
|
auto tmp = std::move(err());
|
||||||
val().~T();
|
err().~unexpected<E>();
|
||||||
::new (errptr()) unexpected<E> (std::move(rhs));
|
|
||||||
this->m_has_val = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return *this;
|
try {
|
||||||
|
::new (valptr()) T(std::forward<Args>(args)...);
|
||||||
|
this->m_has_val = true;
|
||||||
|
} catch (...) {
|
||||||
|
err() = std::move(tmp);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template <class... Args,
|
template <class U, class... Args,
|
||||||
detail::enable_if_t<std::is_nothrow_constructible<T, Args&&...>::value>* = nullptr>
|
detail::enable_if_t<std::is_nothrow_constructible<
|
||||||
void emplace(Args &&... args) {
|
T, std::initializer_list<U> &, Args &&...>::value> * = nullptr>
|
||||||
if (has_value()) {
|
void emplace(std::initializer_list<U> il, Args &&... args) {
|
||||||
val() = T(std::forward<Args>(args)...);
|
if (has_value()) {
|
||||||
}
|
T t(il, std::forward<Args>(args)...);
|
||||||
else {
|
val() = std::move(t);
|
||||||
err().~unexpected<E>();
|
} else {
|
||||||
::new (valptr()) T (std::forward<Args>(args)...);
|
err().~unexpected<E>();
|
||||||
this->m_has_val = true;
|
::new (valptr()) T(il, std::forward<Args>(args)...);
|
||||||
}
|
this->m_has_val = true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template <class... Args,
|
template <class U, class... Args,
|
||||||
detail::enable_if_t<!std::is_nothrow_constructible<T, Args&&...>::value>* = nullptr>
|
detail::enable_if_t<!std::is_nothrow_constructible<
|
||||||
void emplace(Args &&... args) {
|
T, std::initializer_list<U> &, Args &&...>::value> * = nullptr>
|
||||||
if (has_value()) {
|
void emplace(std::initializer_list<U> il, Args &&... args) {
|
||||||
val() = T(std::forward<Args>(args)...);
|
if (has_value()) {
|
||||||
}
|
T t(il, std::forward<Args>(args)...);
|
||||||
else {
|
val() = std::move(t);
|
||||||
auto tmp = std::move(err());
|
} else {
|
||||||
err().~unexpected<E>();
|
auto tmp = std::move(err());
|
||||||
|
err().~unexpected<E>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
::new (valptr()) T (std::forward<Args>(args)...);
|
::new (valptr()) T(il, std::forward<Args>(args)...);
|
||||||
this->m_has_val = true;
|
this->m_has_val = true;
|
||||||
}
|
} catch (...) {
|
||||||
catch (...) {
|
err() = std::move(tmp);
|
||||||
err() = std::move(tmp);
|
throw;
|
||||||
throw;
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class U, class... Args,
|
|
||||||
detail::enable_if_t<std::is_nothrow_constructible<T, std::initializer_list<U>&, Args&&...>::value>* = nullptr>
|
|
||||||
void emplace(std::initializer_list<U> il, Args &&... args) {
|
|
||||||
if (has_value()) {
|
|
||||||
T t (il, std::forward<Args>(args)...);
|
|
||||||
val() = std::move(t);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
err().~unexpected<E>();
|
|
||||||
::new (valptr()) T (il, std::forward<Args>(args)...);
|
|
||||||
this->m_has_val = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <class U, class... Args,
|
|
||||||
detail::enable_if_t<!std::is_nothrow_constructible<T, std::initializer_list<U>&, Args&&...>::value>* = nullptr>
|
|
||||||
void emplace(std::initializer_list<U> il, Args &&... args) {
|
|
||||||
if (has_value()) {
|
|
||||||
T t (il, std::forward<Args>(args)...);
|
|
||||||
val() = std::move(t);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
auto tmp = std::move(err());
|
|
||||||
err().~unexpected<E>();
|
|
||||||
|
|
||||||
try {
|
|
||||||
::new (valptr()) T (il, std::forward<Args>(args)...);
|
|
||||||
this->m_has_val = true;
|
|
||||||
}
|
|
||||||
catch (...) {
|
|
||||||
err() = std::move(tmp);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO SFINAE
|
// TODO SFINAE
|
||||||
void swap(expected &rhs) noexcept(
|
void swap(expected &rhs) noexcept(
|
||||||
@@ -1081,198 +1078,198 @@ public:
|
|||||||
return bool(*this) ? std::move(**this) : static_cast<T>(std::forward<U>(v));
|
return bool(*this) ? std::move(**this) : static_cast<T>(std::forward<U>(v));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template <class U = T, detail::enable_if_t<std::is_nothrow_copy_constructible<U>::value>* = nullptr>
|
template <class U = T,
|
||||||
expected &assign(const expected &rhs) noexcept {
|
detail::enable_if_t<std::is_nothrow_copy_constructible<U>::value>
|
||||||
if (!has_value() && rhs.has_value()) {
|
* = nullptr>
|
||||||
err().~unexpected<E>();
|
expected &assign(const expected &rhs) noexcept {
|
||||||
::new (valptr()) T (*rhs);
|
if (!has_value() && rhs.has_value()) {
|
||||||
this->m_has_val = true;
|
err().~unexpected<E>();
|
||||||
return *this;
|
::new (valptr()) T(*rhs);
|
||||||
}
|
this->m_has_val = true;
|
||||||
|
return *this;
|
||||||
return assign_common(rhs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class U = T, detail::enable_if_t<!std::is_nothrow_copy_constructible<U>::value && std::is_nothrow_move_constructible<U>::value>* = nullptr>
|
return assign_common(rhs);
|
||||||
expected &assign(const expected &rhs) noexcept {
|
}
|
||||||
if (!has_value() && rhs.has_value()) {
|
|
||||||
T tmp = *rhs;
|
|
||||||
err().~unexpected<E>();
|
|
||||||
::new (valptr()) T (std::move(tmp));
|
|
||||||
this->m_has_val = true;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
return assign_common(rhs);
|
template <class U = T,
|
||||||
|
detail::enable_if_t<!std::is_nothrow_copy_constructible<U>::value &&
|
||||||
|
std::is_nothrow_move_constructible<U>::value>
|
||||||
|
* = nullptr>
|
||||||
|
expected &assign(const expected &rhs) noexcept {
|
||||||
|
if (!has_value() && rhs.has_value()) {
|
||||||
|
T tmp = *rhs;
|
||||||
|
err().~unexpected<E>();
|
||||||
|
::new (valptr()) T(std::move(tmp));
|
||||||
|
this->m_has_val = true;
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class U = T, detail::enable_if_t<!std::is_nothrow_copy_constructible<U>::value && !std::is_nothrow_move_constructible<U>::value>* = nullptr>
|
return assign_common(rhs);
|
||||||
expected &assign(const expected &rhs) {
|
}
|
||||||
if (!has_value() && rhs.has_value()) {
|
|
||||||
auto tmp = std::move(err());
|
|
||||||
err().~unexpected<E>();
|
|
||||||
|
|
||||||
try {
|
template <class U = T,
|
||||||
::new (valptr()) T (*rhs);
|
detail::enable_if_t<!std::is_nothrow_copy_constructible<U>::value &&
|
||||||
this->m_has_val = true;
|
!std::is_nothrow_move_constructible<U>::value>
|
||||||
}
|
* = nullptr>
|
||||||
catch(...) {
|
expected &assign(const expected &rhs) {
|
||||||
err() = std::move(tmp);
|
if (!has_value() && rhs.has_value()) {
|
||||||
throw;
|
auto tmp = std::move(err());
|
||||||
}
|
err().~unexpected<E>();
|
||||||
this->m_has_val = true;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
return assign_common(rhs);
|
try {
|
||||||
|
::new (valptr()) T(*rhs);
|
||||||
|
this->m_has_val = true;
|
||||||
|
} catch (...) {
|
||||||
|
err() = std::move(tmp);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
this->m_has_val = true;
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class U = T, detail::enable_if_t<std::is_nothrow_move_constructible<U>::value>* = nullptr>
|
return assign_common(rhs);
|
||||||
expected &assign(expected && rhs) noexcept {
|
}
|
||||||
if (!has_value() && rhs.has_value()) {
|
|
||||||
err().~unexpected<E>();
|
|
||||||
::new (valptr()) T (*std::move(rhs));
|
|
||||||
this->m_has_val = true;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
return assign_common(rhs);
|
template <class U = T,
|
||||||
}
|
detail::enable_if_t<std::is_nothrow_move_constructible<U>::value>
|
||||||
|
* = nullptr>
|
||||||
|
expected &assign(expected &&rhs) noexcept {
|
||||||
|
if (!has_value() && rhs.has_value()) {
|
||||||
|
err().~unexpected<E>();
|
||||||
|
::new (valptr()) T(*std::move(rhs));
|
||||||
|
this->m_has_val = true;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
template <class U = T, detail::enable_if_t<!std::is_nothrow_move_constructible<U>::value>* = nullptr>
|
return assign_common(rhs);
|
||||||
expected &assign(expected && rhs) {
|
}
|
||||||
if (!has_value() && rhs.has_value()) {
|
|
||||||
auto tmp = std::move(err());
|
|
||||||
err().~unexpected<E>();
|
|
||||||
try {
|
|
||||||
::new (valptr()) T (*std::move(rhs));
|
|
||||||
this->m_has_val = true;
|
|
||||||
}
|
|
||||||
catch (...) {
|
|
||||||
err() = std::move(tmp);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
|
|
||||||
return *this;
|
template <class U = T,
|
||||||
}
|
detail::enable_if_t<!std::is_nothrow_move_constructible<U>::value>
|
||||||
|
* = nullptr>
|
||||||
|
expected &assign(expected &&rhs) {
|
||||||
|
if (!has_value() && rhs.has_value()) {
|
||||||
|
auto tmp = std::move(err());
|
||||||
|
err().~unexpected<E>();
|
||||||
|
try {
|
||||||
|
::new (valptr()) T(*std::move(rhs));
|
||||||
|
this->m_has_val = true;
|
||||||
|
} catch (...) {
|
||||||
|
err() = std::move(tmp);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
return assign_common(rhs);
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return assign_common(rhs);
|
||||||
|
}
|
||||||
|
|
||||||
template <class Rhs>
|
template <class Rhs> expected &assign_common(Rhs &&rhs) {
|
||||||
expected& assign_common(Rhs&& rhs) {
|
if (has_value()) {
|
||||||
if (has_value()) {
|
if (rhs.has_value()) {
|
||||||
if (rhs.has_value()) {
|
val() = *std::forward<Rhs>(rhs);
|
||||||
val() = *std::forward<Rhs>(rhs);
|
} else {
|
||||||
}
|
val().~T();
|
||||||
else {
|
::new (errptr()) unexpected<E>(std::forward<Rhs>(rhs).err());
|
||||||
val().~T();
|
}
|
||||||
::new (errptr()) unexpected<E> (std::forward<Rhs>(rhs).err());
|
} else {
|
||||||
}
|
if (!rhs.has_value()) {
|
||||||
}
|
err() = std::forward<Rhs>(rhs).err();
|
||||||
else {
|
}
|
||||||
if (!rhs.has_value()) {
|
}
|
||||||
err() = std::forward<Rhs>(rhs).err();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
template <class Exp> using err_t = typename detail::decay_t<Exp>::error_type;
|
template <class Exp> using err_t = typename detail::decay_t<Exp>::error_type;
|
||||||
template <class Exp, class Ret> using ret_t = expected<Ret, err_t<Exp>>;
|
template <class Exp, class Ret> using ret_t = expected<Ret, err_t<Exp>>;
|
||||||
|
|
||||||
#ifdef TL_EXPECTED_CXX14
|
#ifdef TL_EXPECTED_CXX14
|
||||||
template <class Exp, class F,
|
template <class Exp, class F,
|
||||||
class Ret = decltype(detail::invoke(std::declval<F>(),
|
class Ret = decltype(detail::invoke(std::declval<F>(),
|
||||||
*std::declval<Exp>())),
|
*std::declval<Exp>())),
|
||||||
detail::enable_if_t<!std::is_void<Ret>::value> * = nullptr>
|
detail::enable_if_t<!std::is_void<Ret>::value> * = nullptr>
|
||||||
constexpr auto map_impl(Exp &&exp, F &&f) {
|
constexpr auto map_impl(Exp &&exp, F &&f) {
|
||||||
using result = ret_t<Exp, Ret>;
|
using result = ret_t<Exp, Ret>;
|
||||||
return exp.has_value() ? result(detail::invoke(std::forward<F>(f),
|
return exp.has_value() ? result(detail::invoke(std::forward<F>(f),
|
||||||
*std::forward<Exp>(exp)))
|
*std::forward<Exp>(exp)))
|
||||||
: result(unexpect, std::forward<Exp>(exp).error());
|
: result(unexpect, std::forward<Exp>(exp).error());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class Exp, class F,
|
||||||
|
class Ret = decltype(detail::invoke(std::declval<F>(),
|
||||||
|
*std::declval<Exp>())),
|
||||||
|
detail::enable_if_t<std::is_void<Ret>::value> * = nullptr>
|
||||||
|
auto map_impl(Exp &&exp, F &&f) {
|
||||||
|
using result = expected<monostate, err_t<Exp>>;
|
||||||
|
if (exp.has_value()) {
|
||||||
|
detail::invoke(std::forward<F>(f), *std::forward<Exp>(exp));
|
||||||
|
return result(monostate{});
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Exp, class F,
|
return result(unexpect, std::forward<Exp>(exp).error());
|
||||||
class Ret = decltype(detail::invoke(std::declval<F>(),
|
}
|
||||||
*std::declval<Exp>())),
|
|
||||||
detail::enable_if_t<std::is_void<Ret>::value> * = nullptr>
|
|
||||||
auto map_impl(Exp &&exp, F &&f) {
|
|
||||||
using result = expected<monostate, err_t<Exp>>;
|
|
||||||
if (exp.has_value()) {
|
|
||||||
detail::invoke(std::forward<F>(f), *std::forward<Exp>(exp));
|
|
||||||
return result(monostate{});
|
|
||||||
}
|
|
||||||
|
|
||||||
return result(unexpect, std::forward<Exp>(exp).error());
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
template <class Exp, class F,
|
template <class Exp, class F,
|
||||||
class Ret = decltype(detail::invoke(std::declval<F>(),
|
class Ret = decltype(detail::invoke(std::declval<F>(),
|
||||||
*std::declval<Exp>())),
|
*std::declval<Exp>())),
|
||||||
detail::enable_if_t<!std::is_void<Ret>::value> * = nullptr>
|
detail::enable_if_t<!std::is_void<Ret>::value> * = nullptr>
|
||||||
|
|
||||||
constexpr auto map_impl(Exp &&exp, F &&f) -> ret_t<Exp, Ret> {
|
constexpr auto map_impl(Exp &&exp, F &&f) -> ret_t<Exp, Ret> {
|
||||||
using result = ret_t<Exp, Ret>;
|
using result = ret_t<Exp, Ret>;
|
||||||
|
|
||||||
return exp.has_value() ? result(detail::invoke(std::forward<F>(f),
|
return exp.has_value() ? result(detail::invoke(std::forward<F>(f),
|
||||||
*std::forward<Exp>(exp)))
|
*std::forward<Exp>(exp)))
|
||||||
: result(unexpect, std::forward<Exp>(exp).error());
|
: result(unexpect, std::forward<Exp>(exp).error());
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class Exp, class F,
|
||||||
|
class Ret = decltype(detail::invoke(std::declval<F>(),
|
||||||
|
*std::declval<Exp>())),
|
||||||
|
detail::enable_if_t<std::is_void<Ret>::value> * = nullptr>
|
||||||
|
|
||||||
|
auto map_impl(Exp &&exp, F &&f) -> expected<monostate, err_t<Exp>> {
|
||||||
|
if (exp.has_value()) {
|
||||||
|
detail::invoke(std::forward<F>(f), *std::forward<Exp>(exp));
|
||||||
|
return tl::monostate{};
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class Exp, class F,
|
return unexpected<err_t<Exp>>(std::forward<Exp>(exp).error());
|
||||||
class Ret = decltype(detail::invoke(std::declval<F>(),
|
}
|
||||||
*std::declval<Exp>())),
|
|
||||||
detail::enable_if_t<std::is_void<Ret>::value> * = nullptr>
|
|
||||||
|
|
||||||
auto map_impl(Exp &&exp, F &&f) -> expected<monostate, err_t<Exp>> {
|
|
||||||
if (exp.has_value()) {
|
|
||||||
detail::invoke(std::forward<F>(f), *std::forward<Exp>(exp));
|
|
||||||
return tl::monostate{};
|
|
||||||
}
|
|
||||||
|
|
||||||
return unexpected<err_t<Exp>>(std::forward<Exp>(exp).error());
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && !defined(TL_EXPECTED_GCC54)
|
#if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \
|
||||||
template <class Exp, class F,
|
!defined(TL_EXPECTED_GCC54)
|
||||||
class Ret = decltype(detail::invoke(std::declval<F>(),
|
template <class Exp, class F,
|
||||||
*std::declval<Exp>()))>
|
class Ret = decltype(detail::invoke(std::declval<F>(),
|
||||||
constexpr auto map_error_impl(Exp &&exp, F &&f) {
|
*std::declval<Exp>()))>
|
||||||
using result = ret_t<Exp, Ret>;
|
constexpr auto map_error_impl(Exp &&exp, F &&f) {
|
||||||
return exp.has_value()
|
using result = ret_t<Exp, Ret>;
|
||||||
? result(*std::forward<Exp>(exp))
|
return exp.has_value()
|
||||||
: result(unexpect,
|
? result(*std::forward<Exp>(exp))
|
||||||
detail::invoke(std::forward<F>(f),
|
: result(unexpect, detail::invoke(std::forward<F>(f),
|
||||||
std::forward<Exp>(exp).error()));
|
std::forward<Exp>(exp).error()));
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
template <class Exp, class F,
|
template <class Exp, class F,
|
||||||
class Ret = decltype(detail::invoke(std::declval<F>(),
|
class Ret = decltype(detail::invoke(std::declval<F>(),
|
||||||
*std::declval<Exp>()))>
|
*std::declval<Exp>()))>
|
||||||
constexpr auto map_error_impl(Exp &&exp, F &&f) -> ret_t<Exp, Ret> {
|
constexpr auto map_error_impl(Exp &&exp, F &&f) -> ret_t<Exp, Ret> {
|
||||||
using result = ret_t<Exp, Ret>;
|
using result = ret_t<Exp, Ret>;
|
||||||
|
|
||||||
return exp.has_value()
|
return exp.has_value()
|
||||||
? result(*std::forward<Exp>(exp))
|
? result(*std::forward<Exp>(exp))
|
||||||
: result(unexpect,
|
: result(unexpect, detail::invoke(std::forward<F>(f),
|
||||||
detail::invoke(std::forward<F>(f),
|
std::forward<Exp>(exp).error()));
|
||||||
std::forward<Exp>(exp).error()));
|
}
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
}
|
|
||||||
|
|
||||||
// TODO
|
|
||||||
template <class E> class expected<void, E> {};
|
|
||||||
|
|
||||||
template <class T, class E, class U, class F>
|
template <class T, class E, class U, class F>
|
||||||
constexpr bool operator==(const expected<T, E> &lhs,
|
constexpr bool operator==(const expected<T, E> &lhs,
|
||||||
@@ -1288,14 +1285,32 @@ constexpr bool operator!=(const expected<T, E> &lhs,
|
|||||||
? true
|
? true
|
||||||
: (!lhs.has_value() ? lhs.error() != rhs.error() : *lhs != *rhs);
|
: (!lhs.has_value() ? lhs.error() != rhs.error() : *lhs != *rhs);
|
||||||
}
|
}
|
||||||
template <class T, class E, class U, class F>
|
template <class T, class E, class U, class F>
|
||||||
constexpr bool operator<(const expected<T, E> &lhs, const expected<U, F> &rhs) {
|
constexpr bool operator<(const expected<T, E> &lhs, const expected<U, F> &rhs) {
|
||||||
return (lhs.has_value() != rhs.has_value())
|
return (lhs.has_value() != rhs.has_value())
|
||||||
? true
|
? (!lhs.has_value() ? true : false)
|
||||||
: (!lhs.has_value() ? lhs.error() != rhs.error() : *lhs != *rhs);
|
: (!lhs.has_value() ? lhs.error() < rhs.error() : *lhs < *rhs);
|
||||||
|
}
|
||||||
|
template <class T, class E, class U, class F>
|
||||||
|
constexpr bool operator>(const expected<T, E> &lhs, const expected<U, F> &rhs) {
|
||||||
|
return (lhs.has_value() != rhs.has_value())
|
||||||
|
? (!lhs.has_value() ? false : true)
|
||||||
|
: (!lhs.has_value() ? lhs.error() > rhs.error() : *lhs > *rhs);
|
||||||
|
}
|
||||||
|
template <class T, class E, class U, class F>
|
||||||
|
constexpr bool operator<=(const expected<T, E> &lhs,
|
||||||
|
const expected<U, F> &rhs) {
|
||||||
|
return (lhs.has_value() != rhs.has_value())
|
||||||
|
? (!lhs.has_value() ? false : true)
|
||||||
|
: (!lhs.has_value() ? lhs.error() <= rhs.error() : *lhs <= *rhs);
|
||||||
|
}
|
||||||
|
template <class T, class E, class U, class F>
|
||||||
|
constexpr bool operator>=(const expected<T, E> &lhs,
|
||||||
|
const expected<U, F> &rhs) {
|
||||||
|
return (lhs.has_value() != rhs.has_value())
|
||||||
|
? (!lhs.has_value() ? true : false)
|
||||||
|
: (!lhs.has_value() ? lhs.error() >= rhs.error() : *lhs >= *rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO others
|
|
||||||
|
|
||||||
template <class T, class E, class U>
|
template <class T, class E, class U>
|
||||||
constexpr bool operator==(const expected<T, E> &x, const U &v) {
|
constexpr bool operator==(const expected<T, E> &x, const U &v) {
|
||||||
@@ -1346,8 +1361,6 @@ constexpr bool operator>=(const U &v, const expected<T, E> &x) {
|
|||||||
return x.has_value() ? v >= *x : true;
|
return x.has_value() ? v >= *x : true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO others
|
|
||||||
|
|
||||||
template <class T, class E>
|
template <class T, class E>
|
||||||
constexpr bool operator==(const expected<T, E> &x, const unexpected<E> &e) {
|
constexpr bool operator==(const expected<T, E> &x, const unexpected<E> &e) {
|
||||||
return x.has_value() ? true : x.error() == e.value();
|
return x.has_value() ? true : x.error() == e.value();
|
||||||
|
Reference in New Issue
Block a user