forked from TartanLlama/expected
More comparisons
This commit is contained in:
721
expected.hpp
721
expected.hpp
@@ -16,9 +16,9 @@
|
||||
|
||||
#include <exception>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <iostream>
|
||||
|
||||
#if (defined(_MSC_VER) && _MSC_VER == 1900)
|
||||
#define TL_EXPECTED_MSVC2015
|
||||
@@ -163,9 +163,8 @@ constexpr bool operator>=(const unexpected<E> &lhs, const unexpected<E> &rhs) {
|
||||
return lhs.value() >= rhs.value();
|
||||
}
|
||||
|
||||
template <class E>
|
||||
unexpected<E> make_unexpected (E&& e) {
|
||||
return unexpected<E>(std::forward<E>(e));
|
||||
template <class E> unexpected<E> make_unexpected(E &&e) {
|
||||
return unexpected<E>(std::forward<E>(e));
|
||||
}
|
||||
|
||||
struct unexpect_t {
|
||||
@@ -398,51 +397,52 @@ template <class E> struct expected_storage_base<void, E, false, false> {
|
||||
};
|
||||
|
||||
// TODO, conditionally delete things
|
||||
template <class T, class E> struct expected_ctor_base {};
|
||||
template <class T, class E,
|
||||
bool = (std::is_copy_assignable<T>::value && std::is_copy_assignable<E>::value &&
|
||||
std::is_copy_constructible<E>::value && std::is_copy_constructible<T>::value &&
|
||||
std::is_nothrow_move_constructible<E>::value),
|
||||
bool = (std::is_move_constructible<T>::value && std::is_move_assignable<T>::value &&
|
||||
std::is_nothrow_move_constructible<E>::value && std::is_nothrow_move_assignable<E>::value)>
|
||||
struct expected_assign_base {
|
||||
expected_assign_base() = 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> struct expected_ctor_base {};
|
||||
template <class T, class E,
|
||||
bool = (std::is_copy_assignable<T>::value &&
|
||||
std::is_copy_assignable<E>::value &&
|
||||
std::is_copy_constructible<E>::value &&
|
||||
std::is_copy_constructible<T>::value &&
|
||||
std::is_nothrow_move_constructible<E>::value),
|
||||
bool = (std::is_move_constructible<T>::value &&
|
||||
std::is_move_assignable<T>::value &&
|
||||
std::is_nothrow_move_constructible<E>::value &&
|
||||
std::is_nothrow_move_assignable<E>::value)>
|
||||
struct expected_assign_base {
|
||||
expected_assign_base() = 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>
|
||||
struct expected_assign_base<T,E,true,false> {
|
||||
expected_assign_base() = 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 = delete;
|
||||
};
|
||||
template <class T, class E> struct expected_assign_base<T, E, true, false> {
|
||||
expected_assign_base() = 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 = delete;
|
||||
};
|
||||
|
||||
template <class T, class E>
|
||||
struct expected_assign_base<T,E,false,true> {
|
||||
expected_assign_base() = 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&) = delete;
|
||||
expected_assign_base& operator=(expected_assign_base&&) noexcept = default;
|
||||
};
|
||||
template <class T, class E> struct expected_assign_base<T, E, false, true> {
|
||||
expected_assign_base() = 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 &) = delete;
|
||||
expected_assign_base &operator=(expected_assign_base &&) noexcept = default;
|
||||
};
|
||||
|
||||
template <class T, class E>
|
||||
struct expected_assign_base<T,E,false,false> {
|
||||
expected_assign_base() = 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&) = delete;
|
||||
expected_assign_base& operator=(expected_assign_base&&) noexcept = delete;
|
||||
};
|
||||
template <class T, class E> struct expected_assign_base<T, E, false, false> {
|
||||
expected_assign_base() = 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 &) = delete;
|
||||
expected_assign_base &operator=(expected_assign_base &&) noexcept = delete;
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
template <class E> class bad_expected_access : public std::exception {
|
||||
@@ -464,8 +464,7 @@ private:
|
||||
|
||||
template <class T, class 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_same<T, std::remove_cv<in_place_t>>::value,
|
||||
"T must not be in_place_t");
|
||||
@@ -490,7 +489,8 @@ public:
|
||||
typedef E error_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
|
||||
/// Carries out some operation which returns an optional on the stored object
|
||||
/// if there is one. \requires `std::invoke(std::forward<F>(f), value())`
|
||||
@@ -604,7 +604,8 @@ public:
|
||||
#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.
|
||||
/// \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
|
||||
@@ -664,7 +665,8 @@ public:
|
||||
#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.
|
||||
/// \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
|
||||
@@ -753,7 +755,8 @@ public:
|
||||
detail::enable_if_t<std::is_constructible<E, const G &>::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 <
|
||||
class G = E,
|
||||
@@ -853,163 +856,157 @@ public:
|
||||
constexpr expected(U &&v) : expected(in_place, std::forward<U>(v)) {}
|
||||
|
||||
// TODO conditionally delete
|
||||
expected& operator=(const expected& rhs) {
|
||||
return assign(rhs);
|
||||
expected &operator=(const expected &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 assign(std::move(rhs));
|
||||
return *this;
|
||||
}
|
||||
|
||||
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,
|
||||
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;
|
||||
}
|
||||
|
||||
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,
|
||||
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;
|
||||
}
|
||||
|
||||
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,
|
||||
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;
|
||||
}
|
||||
|
||||
return *this;
|
||||
template <class... Args, detail::enable_if_t<std::is_nothrow_constructible<
|
||||
T, Args &&...>::value> * = nullptr>
|
||||
void emplace(Args &&... args) {
|
||||
if (has_value()) {
|
||||
val() = T(std::forward<Args>(args)...);
|
||||
} else {
|
||||
err().~unexpected<E>();
|
||||
::new (valptr()) T(std::forward<Args>(args)...);
|
||||
this->m_has_val = true;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
template <class... Args, detail::enable_if_t<!std::is_nothrow_constructible<
|
||||
T, Args &&...>::value> * = nullptr>
|
||||
void emplace(Args &&... args) {
|
||||
if (has_value()) {
|
||||
val() = T(std::forward<Args>(args)...);
|
||||
} else {
|
||||
auto tmp = std::move(err());
|
||||
err().~unexpected<E>();
|
||||
|
||||
return *this;
|
||||
try {
|
||||
::new (valptr()) T(std::forward<Args>(args)...);
|
||||
this->m_has_val = true;
|
||||
} catch (...) {
|
||||
err() = std::move(tmp);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <class... Args,
|
||||
detail::enable_if_t<std::is_nothrow_constructible<T, Args&&...>::value>* = nullptr>
|
||||
void emplace(Args &&... args) {
|
||||
if (has_value()) {
|
||||
val() = T(std::forward<Args>(args)...);
|
||||
}
|
||||
else {
|
||||
err().~unexpected<E>();
|
||||
::new (valptr()) T (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 {
|
||||
err().~unexpected<E>();
|
||||
::new (valptr()) T(il, std::forward<Args>(args)...);
|
||||
this->m_has_val = true;
|
||||
}
|
||||
}
|
||||
|
||||
template <class... Args,
|
||||
detail::enable_if_t<!std::is_nothrow_constructible<T, Args&&...>::value>* = nullptr>
|
||||
void emplace(Args &&... args) {
|
||||
if (has_value()) {
|
||||
val() = T(std::forward<Args>(args)...);
|
||||
}
|
||||
else {
|
||||
auto tmp = std::move(err());
|
||||
err().~unexpected<E>();
|
||||
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 (std::forward<Args>(args)...);
|
||||
this->m_has_val = true;
|
||||
}
|
||||
catch (...) {
|
||||
err() = std::move(tmp);
|
||||
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;
|
||||
}
|
||||
}
|
||||
try {
|
||||
::new (valptr()) T(il, std::forward<Args>(args)...);
|
||||
this->m_has_val = true;
|
||||
} catch (...) {
|
||||
err() = std::move(tmp);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO SFINAE
|
||||
void swap(expected &rhs) noexcept(
|
||||
@@ -1081,198 +1078,198 @@ public:
|
||||
return bool(*this) ? std::move(**this) : static_cast<T>(std::forward<U>(v));
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
template <class U = T, detail::enable_if_t<std::is_nothrow_copy_constructible<U>::value>* = nullptr>
|
||||
expected &assign(const expected &rhs) noexcept {
|
||||
if (!has_value() && rhs.has_value()) {
|
||||
err().~unexpected<E>();
|
||||
::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>
|
||||
* = nullptr>
|
||||
expected &assign(const expected &rhs) noexcept {
|
||||
if (!has_value() && rhs.has_value()) {
|
||||
err().~unexpected<E>();
|
||||
::new (valptr()) T(*rhs);
|
||||
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>
|
||||
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);
|
||||
}
|
||||
|
||||
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>
|
||||
expected &assign(const expected &rhs) {
|
||||
if (!has_value() && rhs.has_value()) {
|
||||
auto tmp = std::move(err());
|
||||
err().~unexpected<E>();
|
||||
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_copy_constructible<U>::value &&
|
||||
!std::is_nothrow_move_constructible<U>::value>
|
||||
* = nullptr>
|
||||
expected &assign(const expected &rhs) {
|
||||
if (!has_value() && rhs.has_value()) {
|
||||
auto tmp = std::move(err());
|
||||
err().~unexpected<E>();
|
||||
|
||||
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>
|
||||
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);
|
||||
}
|
||||
|
||||
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>
|
||||
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;
|
||||
}
|
||||
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>
|
||||
expected& assign_common(Rhs&& rhs) {
|
||||
if (has_value()) {
|
||||
if (rhs.has_value()) {
|
||||
val() = *std::forward<Rhs>(rhs);
|
||||
}
|
||||
else {
|
||||
val().~T();
|
||||
::new (errptr()) unexpected<E> (std::forward<Rhs>(rhs).err());
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!rhs.has_value()) {
|
||||
err() = std::forward<Rhs>(rhs).err();
|
||||
}
|
||||
}
|
||||
template <class Rhs> expected &assign_common(Rhs &&rhs) {
|
||||
if (has_value()) {
|
||||
if (rhs.has_value()) {
|
||||
val() = *std::forward<Rhs>(rhs);
|
||||
} else {
|
||||
val().~T();
|
||||
::new (errptr()) unexpected<E>(std::forward<Rhs>(rhs).err());
|
||||
}
|
||||
} else {
|
||||
if (!rhs.has_value()) {
|
||||
err() = std::forward<Rhs>(rhs).err();
|
||||
}
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
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>>;
|
||||
namespace detail {
|
||||
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>>;
|
||||
|
||||
#ifdef TL_EXPECTED_CXX14
|
||||
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>
|
||||
constexpr auto map_impl(Exp &&exp, F &&f) {
|
||||
using result = ret_t<Exp, Ret>;
|
||||
return exp.has_value() ? result(detail::invoke(std::forward<F>(f),
|
||||
*std::forward<Exp>(exp)))
|
||||
: 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>
|
||||
constexpr auto map_impl(Exp &&exp, F &&f) {
|
||||
using result = ret_t<Exp, Ret>;
|
||||
return exp.has_value() ? result(detail::invoke(std::forward<F>(f),
|
||||
*std::forward<Exp>(exp)))
|
||||
: 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,
|
||||
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());
|
||||
}
|
||||
return result(unexpect, std::forward<Exp>(exp).error());
|
||||
}
|
||||
#else
|
||||
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>
|
||||
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>
|
||||
|
||||
constexpr auto map_impl(Exp &&exp, F &&f) -> ret_t<Exp, Ret> {
|
||||
using result = ret_t<Exp, Ret>;
|
||||
constexpr auto map_impl(Exp &&exp, F &&f) -> ret_t<Exp, Ret> {
|
||||
using result = ret_t<Exp, Ret>;
|
||||
|
||||
return exp.has_value() ? result(detail::invoke(std::forward<F>(f),
|
||||
*std::forward<Exp>(exp)))
|
||||
: result(unexpect, std::forward<Exp>(exp).error());
|
||||
return exp.has_value() ? result(detail::invoke(std::forward<F>(f),
|
||||
*std::forward<Exp>(exp)))
|
||||
: 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,
|
||||
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());
|
||||
}
|
||||
return unexpected<err_t<Exp>>(std::forward<Exp>(exp).error());
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && !defined(TL_EXPECTED_GCC54)
|
||||
template <class Exp, class F,
|
||||
class Ret = decltype(detail::invoke(std::declval<F>(),
|
||||
*std::declval<Exp>()))>
|
||||
constexpr auto map_error_impl(Exp &&exp, F &&f) {
|
||||
using result = ret_t<Exp, Ret>;
|
||||
return exp.has_value()
|
||||
? result(*std::forward<Exp>(exp))
|
||||
: result(unexpect,
|
||||
detail::invoke(std::forward<F>(f),
|
||||
std::forward<Exp>(exp).error()));
|
||||
}
|
||||
#if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \
|
||||
!defined(TL_EXPECTED_GCC54)
|
||||
template <class Exp, class F,
|
||||
class Ret = decltype(detail::invoke(std::declval<F>(),
|
||||
*std::declval<Exp>()))>
|
||||
constexpr auto map_error_impl(Exp &&exp, F &&f) {
|
||||
using result = ret_t<Exp, Ret>;
|
||||
return exp.has_value()
|
||||
? result(*std::forward<Exp>(exp))
|
||||
: result(unexpect, detail::invoke(std::forward<F>(f),
|
||||
std::forward<Exp>(exp).error()));
|
||||
}
|
||||
#else
|
||||
template <class Exp, class F,
|
||||
class Ret = decltype(detail::invoke(std::declval<F>(),
|
||||
*std::declval<Exp>()))>
|
||||
constexpr auto map_error_impl(Exp &&exp, F &&f) -> ret_t<Exp, Ret> {
|
||||
using result = ret_t<Exp, Ret>;
|
||||
template <class Exp, class F,
|
||||
class Ret = decltype(detail::invoke(std::declval<F>(),
|
||||
*std::declval<Exp>()))>
|
||||
constexpr auto map_error_impl(Exp &&exp, F &&f) -> ret_t<Exp, Ret> {
|
||||
using result = ret_t<Exp, Ret>;
|
||||
|
||||
return exp.has_value()
|
||||
? result(*std::forward<Exp>(exp))
|
||||
: result(unexpect,
|
||||
detail::invoke(std::forward<F>(f),
|
||||
std::forward<Exp>(exp).error()));
|
||||
}
|
||||
return exp.has_value()
|
||||
? result(*std::forward<Exp>(exp))
|
||||
: result(unexpect, detail::invoke(std::forward<F>(f),
|
||||
std::forward<Exp>(exp).error()));
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
}
|
||||
|
||||
// TODO
|
||||
template <class E> class expected<void, E> {};
|
||||
} // namespace detail
|
||||
|
||||
template <class T, class E, class U, class F>
|
||||
constexpr bool operator==(const expected<T, E> &lhs,
|
||||
@@ -1288,14 +1285,32 @@ constexpr bool operator!=(const expected<T, E> &lhs,
|
||||
? true
|
||||
: (!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) {
|
||||
return (lhs.has_value() != rhs.has_value())
|
||||
? true
|
||||
: (!lhs.has_value() ? lhs.error() != rhs.error() : *lhs != *rhs);
|
||||
? (!lhs.has_value() ? true : false)
|
||||
: (!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>
|
||||
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;
|
||||
}
|
||||
|
||||
// TODO others
|
||||
|
||||
template <class T, class E>
|
||||
constexpr bool operator==(const expected<T, E> &x, const unexpected<E> &e) {
|
||||
return x.has_value() ? true : x.error() == e.value();
|
||||
|
Reference in New Issue
Block a user