mirror of
https://github.com/TartanLlama/expected.git
synced 2025-08-04 03:14:29 +02:00
Clang format
This commit is contained in:
733
expected.hpp
733
expected.hpp
@@ -14,108 +14,109 @@
|
|||||||
#ifndef TL_EXPECTED_HPP
|
#ifndef TL_EXPECTED_HPP
|
||||||
#define TL_EXPECTED_HPP
|
#define TL_EXPECTED_HPP
|
||||||
|
|
||||||
|
#include <exception>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <exception>
|
|
||||||
|
|
||||||
namespace tl {
|
namespace tl {
|
||||||
namespace detail {
|
namespace detail {
|
||||||
template <bool E, class T = void>
|
template <bool E, class T = void>
|
||||||
using enable_if_t = typename std::enable_if<E, T>::type;
|
using enable_if_t = typename std::enable_if<E, T>::type;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef TL_IN_PLACE_T_DEFINED
|
#ifndef TL_IN_PLACE_T_DEFINED
|
||||||
#define TL_IN_PLACE_T_DEFINED
|
#define TL_IN_PLACE_T_DEFINED
|
||||||
/// \brief A tag type to tell optional to construct its value in-place
|
/// \brief A tag type to tell optional to construct its value in-place
|
||||||
struct in_place_t {
|
struct in_place_t {
|
||||||
explicit in_place_t() = default;
|
explicit in_place_t() = default;
|
||||||
};
|
};
|
||||||
/// \brief A tag to tell optional to construct its value in-place
|
/// \brief A tag to tell optional to construct its value in-place
|
||||||
static constexpr in_place_t in_place{};
|
static constexpr in_place_t in_place{};
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template <class E>
|
template <class E> class unexpected {
|
||||||
class unexpected {
|
public:
|
||||||
public:
|
|
||||||
static_assert(!std::is_same<E, void>::value, "E must not be void");
|
static_assert(!std::is_same<E, void>::value, "E must not be void");
|
||||||
unexpected() = delete;
|
unexpected() = delete;
|
||||||
constexpr explicit unexpected(const E& e)
|
constexpr explicit unexpected(const E &e) : m_val(e) {}
|
||||||
: m_val(e) {}
|
|
||||||
|
|
||||||
constexpr explicit unexpected(E&& e)
|
constexpr explicit unexpected(E &&e) : m_val(std::move(e)) {}
|
||||||
: m_val(std::move(e)) {}
|
|
||||||
|
|
||||||
constexpr const E& value() const & { return m_val; }
|
constexpr const E &value() const & { return m_val; }
|
||||||
constexpr E& value() & { return m_val; }
|
constexpr E &value() & { return m_val; }
|
||||||
constexpr E&& value() && { return std::move(m_val); }
|
constexpr E &&value() && { return std::move(m_val); }
|
||||||
constexpr E const&& value() const&& { return std::move(m_val); }
|
constexpr E const &&value() const && { return std::move(m_val); }
|
||||||
private:
|
|
||||||
E m_val; // exposition only
|
|
||||||
};
|
|
||||||
|
|
||||||
template <class E>
|
private:
|
||||||
constexpr bool
|
E m_val;
|
||||||
operator==(const unexpected<E>& lhs, const unexpected<E>& rhs) {
|
};
|
||||||
|
|
||||||
|
template <class E>
|
||||||
|
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>
|
||||||
constexpr bool
|
constexpr bool operator!=(const unexpected<E> &lhs, const unexpected<E> &rhs) {
|
||||||
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>
|
||||||
constexpr bool
|
constexpr bool operator<(const unexpected<E> &lhs, const unexpected<E> &rhs) {
|
||||||
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>
|
||||||
constexpr bool
|
constexpr bool operator<=(const unexpected<E> &lhs, const unexpected<E> &rhs) {
|
||||||
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>
|
||||||
constexpr bool
|
constexpr bool operator>(const unexpected<E> &lhs, const unexpected<E> &rhs) {
|
||||||
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>
|
||||||
constexpr bool
|
constexpr bool operator>=(const unexpected<E> &lhs, const unexpected<E> &rhs) {
|
||||||
operator>=(const unexpected<E>& lhs, const unexpected<E>& rhs) {
|
|
||||||
return lhs.value() >= rhs.value();
|
return lhs.value() >= rhs.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
struct unexpect_t {
|
struct unexpect_t {
|
||||||
unexpect_t() = default;
|
unexpect_t() = default;
|
||||||
};
|
};
|
||||||
static constexpr unexpect_t unexpect{};
|
static constexpr unexpect_t unexpect{};
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
template <class T, class E, bool = std::is_trivially_destructible<T>::value, bool = std::is_trivially_destructible<E>::value>
|
template <class T, class E, bool = std::is_trivially_destructible<T>::value,
|
||||||
struct expected_storage_base {
|
bool = std::is_trivially_destructible<E>::value>
|
||||||
constexpr expected_storage_base()
|
struct expected_storage_base {
|
||||||
: m_val(T{}), m_has_val(true) {}
|
constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {}
|
||||||
|
|
||||||
template <class... Args, detail::enable_if_t<std::is_constructible<T, Args&&...>::value>* = nullptr>
|
template <class... Args,
|
||||||
constexpr expected_storage_base(in_place_t, Args&&... args) :
|
detail::enable_if_t<std::is_constructible<T, Args &&...>::value> * =
|
||||||
m_val(std::forward<Args>(args)...), m_has_val(true) {}
|
nullptr>
|
||||||
|
constexpr expected_storage_base(in_place_t, Args &&... args)
|
||||||
|
: m_val(std::forward<Args>(args)...), m_has_val(true) {}
|
||||||
|
|
||||||
template <class U, class... Args, detail::enable_if_t<std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value>* = nullptr>
|
template <class U, class... Args,
|
||||||
constexpr expected_storage_base (in_place_t, std::initializer_list<U> il, Args&&... args) :
|
detail::enable_if_t<std::is_constructible<
|
||||||
m_val(il, std::forward<Args>(args)...), m_has_val(true) {}
|
T, std::initializer_list<U> &, Args &&...>::value> * = nullptr>
|
||||||
template <class... Args, detail::enable_if_t<std::is_constructible<E, Args&&...>::value>* = nullptr>
|
constexpr expected_storage_base(in_place_t, std::initializer_list<U> il,
|
||||||
constexpr explicit expected_storage_base(unexpect_t, Args&&... args) :
|
Args &&... args)
|
||||||
m_unexpect(std::forward<Args>(args)...), m_has_val(false) {}
|
: m_val(il, std::forward<Args>(args)...), m_has_val(true) {}
|
||||||
|
template <class... Args,
|
||||||
template <class U, class... Args, detail::enable_if_t<std::is_constructible<E, std::initializer_list<U>&, Args&&...>::value>* = nullptr>
|
detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * =
|
||||||
constexpr explicit expected_storage_base(unexpect_t, std::initializer_list<U> il, Args&&... args) :
|
nullptr>
|
||||||
m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {}
|
constexpr explicit expected_storage_base(unexpect_t, Args &&... args)
|
||||||
|
: m_unexpect(std::forward<Args>(args)...), m_has_val(false) {}
|
||||||
|
|
||||||
|
template <class U, class... Args,
|
||||||
|
detail::enable_if_t<std::is_constructible<
|
||||||
|
E, std::initializer_list<U> &, Args &&...>::value> * = nullptr>
|
||||||
|
constexpr explicit expected_storage_base(unexpect_t,
|
||||||
|
std::initializer_list<U> il,
|
||||||
|
Args &&... args)
|
||||||
|
: m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {}
|
||||||
|
|
||||||
~expected_storage_base() {
|
~expected_storage_base() {
|
||||||
if (m_has_val) {
|
if (m_has_val) {
|
||||||
m_val.~T();
|
m_val.~T();
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
m_unexpect.~unexpected<E>();
|
m_unexpect.~unexpected<E>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -124,29 +125,36 @@ namespace detail {
|
|||||||
T m_val;
|
T m_val;
|
||||||
unexpected<E> m_unexpect;
|
unexpected<E> m_unexpect;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class T, class E>
|
template <class T, class E> struct expected_storage_base<T, E, true, true> {
|
||||||
struct expected_storage_base<T,E,true,true> {
|
constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {}
|
||||||
constexpr expected_storage_base()
|
|
||||||
: m_val(T{}), m_has_val(true) {}
|
|
||||||
|
|
||||||
|
template <class... Args,
|
||||||
|
detail::enable_if_t<std::is_constructible<T, Args &&...>::value> * =
|
||||||
|
nullptr>
|
||||||
|
constexpr expected_storage_base(in_place_t, Args &&... args)
|
||||||
|
: m_val(std::forward<Args>(args)...), m_has_val(true) {}
|
||||||
|
|
||||||
template <class... Args, detail::enable_if_t<std::is_constructible<T, Args&&...>::value>* = nullptr>
|
template <class U, class... Args,
|
||||||
constexpr expected_storage_base(in_place_t, Args&&... args) :
|
detail::enable_if_t<std::is_constructible<
|
||||||
m_val(std::forward<Args>(args)...), m_has_val(true) {}
|
T, std::initializer_list<U> &, Args &&...>::value> * = nullptr>
|
||||||
|
constexpr expected_storage_base(in_place_t, std::initializer_list<U> il,
|
||||||
template <class U, class... Args, detail::enable_if_t<std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value>* = nullptr>
|
Args &&... args)
|
||||||
constexpr expected_storage_base (in_place_t, std::initializer_list<U> il, Args&&... args) :
|
: m_val(il, std::forward<Args>(args)...), m_has_val(true) {}
|
||||||
m_val(il, std::forward<Args>(args)...), m_has_val(true) {}
|
template <class... Args,
|
||||||
template <class... Args, detail::enable_if_t<std::is_constructible<E, Args&&...>::value>* = nullptr>
|
detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * =
|
||||||
constexpr explicit expected_storage_base(unexpect_t, Args&&... args) :
|
nullptr>
|
||||||
m_unexpect(std::forward<Args>(args)...), m_has_val(false) {}
|
constexpr explicit expected_storage_base(unexpect_t, Args &&... args)
|
||||||
|
: m_unexpect(std::forward<Args>(args)...), m_has_val(false) {}
|
||||||
template <class U, class... Args, detail::enable_if_t<std::is_constructible<E, std::initializer_list<U>&, Args&&...>::value>* = nullptr>
|
|
||||||
constexpr explicit expected_storage_base(unexpect_t, std::initializer_list<U> il, Args&&... args) :
|
|
||||||
m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {}
|
|
||||||
|
|
||||||
|
template <class U, class... Args,
|
||||||
|
detail::enable_if_t<std::is_constructible<
|
||||||
|
E, std::initializer_list<U> &, Args &&...>::value> * = nullptr>
|
||||||
|
constexpr explicit expected_storage_base(unexpect_t,
|
||||||
|
std::initializer_list<U> il,
|
||||||
|
Args &&... args)
|
||||||
|
: m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {}
|
||||||
|
|
||||||
~expected_storage_base() = default;
|
~expected_storage_base() = default;
|
||||||
bool m_has_val;
|
bool m_has_val;
|
||||||
@@ -154,29 +162,36 @@ namespace detail {
|
|||||||
T m_val;
|
T m_val;
|
||||||
unexpected<E> m_unexpect;
|
unexpected<E> m_unexpect;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class T, class E>
|
template <class T, class E> struct expected_storage_base<T, E, true, false> {
|
||||||
struct expected_storage_base<T,E,true,false> {
|
constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {}
|
||||||
constexpr expected_storage_base()
|
|
||||||
: m_val(T{}), m_has_val(true) {}
|
|
||||||
|
|
||||||
|
template <class... Args,
|
||||||
|
detail::enable_if_t<std::is_constructible<T, Args &&...>::value> * =
|
||||||
|
nullptr>
|
||||||
|
constexpr expected_storage_base(in_place_t, Args &&... args)
|
||||||
|
: m_val(std::forward<Args>(args)...), m_has_val(true) {}
|
||||||
|
|
||||||
template <class... Args, detail::enable_if_t<std::is_constructible<T, Args&&...>::value>* = nullptr>
|
template <class U, class... Args,
|
||||||
constexpr expected_storage_base(in_place_t, Args&&... args) :
|
detail::enable_if_t<std::is_constructible<
|
||||||
m_val(std::forward<Args>(args)...), m_has_val(true) {}
|
T, std::initializer_list<U> &, Args &&...>::value> * = nullptr>
|
||||||
|
constexpr expected_storage_base(in_place_t, std::initializer_list<U> il,
|
||||||
template <class U, class... Args, detail::enable_if_t<std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value>* = nullptr>
|
Args &&... args)
|
||||||
constexpr expected_storage_base (in_place_t, std::initializer_list<U> il, Args&&... args) :
|
: m_val(il, std::forward<Args>(args)...), m_has_val(true) {}
|
||||||
m_val(il, std::forward<Args>(args)...), m_has_val(true) {}
|
template <class... Args,
|
||||||
template <class... Args, detail::enable_if_t<std::is_constructible<E, Args&&...>::value>* = nullptr>
|
detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * =
|
||||||
constexpr explicit expected_storage_base(unexpect_t, Args&&... args) :
|
nullptr>
|
||||||
m_unexpect(std::forward<Args>(args)...), m_has_val(false) {}
|
constexpr explicit expected_storage_base(unexpect_t, Args &&... args)
|
||||||
|
: m_unexpect(std::forward<Args>(args)...), m_has_val(false) {}
|
||||||
template <class U, class... Args, detail::enable_if_t<std::is_constructible<E, std::initializer_list<U>&, Args&&...>::value>* = nullptr>
|
|
||||||
constexpr explicit expected_storage_base(unexpect_t, std::initializer_list<U> il, Args&&... args) :
|
|
||||||
m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {}
|
|
||||||
|
|
||||||
|
template <class U, class... Args,
|
||||||
|
detail::enable_if_t<std::is_constructible<
|
||||||
|
E, std::initializer_list<U> &, Args &&...>::value> * = nullptr>
|
||||||
|
constexpr explicit expected_storage_base(unexpect_t,
|
||||||
|
std::initializer_list<U> il,
|
||||||
|
Args &&... args)
|
||||||
|
: m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {}
|
||||||
|
|
||||||
~expected_storage_base() {
|
~expected_storage_base() {
|
||||||
if (!m_has_val) {
|
if (!m_has_val) {
|
||||||
@@ -189,30 +204,36 @@ namespace detail {
|
|||||||
T m_val;
|
T m_val;
|
||||||
unexpected<E> m_unexpect;
|
unexpected<E> m_unexpect;
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
|
||||||
};
|
template <class T, class E> struct expected_storage_base<T, E, false, true> {
|
||||||
|
constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {}
|
||||||
|
|
||||||
template <class T, class E>
|
template <class... Args,
|
||||||
struct expected_storage_base<T,E,false,true> {
|
detail::enable_if_t<std::is_constructible<T, Args &&...>::value> * =
|
||||||
constexpr expected_storage_base()
|
nullptr>
|
||||||
: m_val(T{}), m_has_val(true) {}
|
constexpr expected_storage_base(in_place_t, Args &&... args)
|
||||||
|
: m_val(std::forward<Args>(args)...), m_has_val(true) {}
|
||||||
|
|
||||||
|
template <class U, class... Args,
|
||||||
|
detail::enable_if_t<std::is_constructible<
|
||||||
|
T, std::initializer_list<U> &, Args &&...>::value> * = nullptr>
|
||||||
|
constexpr expected_storage_base(in_place_t, std::initializer_list<U> il,
|
||||||
|
Args &&... args)
|
||||||
|
: m_val(il, std::forward<Args>(args)...), m_has_val(true) {}
|
||||||
|
template <class... Args,
|
||||||
|
detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * =
|
||||||
|
nullptr>
|
||||||
|
constexpr explicit expected_storage_base(unexpect_t, Args &&... args)
|
||||||
|
: m_unexpect(std::forward<Args>(args)...), m_has_val(false) {}
|
||||||
|
|
||||||
template <class... Args, detail::enable_if_t<std::is_constructible<T, Args&&...>::value>* = nullptr>
|
template <class U, class... Args,
|
||||||
constexpr expected_storage_base(in_place_t, Args&&... args) :
|
detail::enable_if_t<std::is_constructible<
|
||||||
m_val(std::forward<Args>(args)...), m_has_val(true) {}
|
E, std::initializer_list<U> &, Args &&...>::value> * = nullptr>
|
||||||
|
constexpr explicit expected_storage_base(unexpect_t,
|
||||||
template <class U, class... Args, detail::enable_if_t<std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value>* = nullptr>
|
std::initializer_list<U> il,
|
||||||
constexpr expected_storage_base(in_place_t, std::initializer_list<U> il, Args&&... args) :
|
Args &&... args)
|
||||||
m_val(il, std::forward<Args>(args)...), m_has_val(true) {}
|
: m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {}
|
||||||
template <class... Args, detail::enable_if_t<std::is_constructible<E, Args&&...>::value>* = nullptr>
|
|
||||||
constexpr explicit expected_storage_base(unexpect_t, Args&&... args) :
|
|
||||||
m_unexpect(std::forward<Args>(args)...), m_has_val(false) {}
|
|
||||||
|
|
||||||
template <class U, class... Args, detail::enable_if_t<std::is_constructible<E, std::initializer_list<U>&, Args&&...>::value>* = nullptr>
|
|
||||||
constexpr explicit expected_storage_base(unexpect_t, std::initializer_list<U> il, Args&&... args) :
|
|
||||||
m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {}
|
|
||||||
|
|
||||||
|
|
||||||
~expected_storage_base() {
|
~expected_storage_base() {
|
||||||
if (m_has_val) {
|
if (m_has_val) {
|
||||||
@@ -224,46 +245,51 @@ namespace detail {
|
|||||||
T m_val;
|
T m_val;
|
||||||
unexpected<E> m_unexpect;
|
unexpected<E> m_unexpect;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class E>
|
template <class E> struct expected_storage_base<void, E, false, true> {
|
||||||
struct expected_storage_base<void,E,false,true> {
|
constexpr expected_storage_base() : m_val(), m_has_val(true) {}
|
||||||
constexpr expected_storage_base()
|
|
||||||
: m_val(), m_has_val(true) {}
|
|
||||||
|
|
||||||
|
template <class... Args,
|
||||||
|
detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * =
|
||||||
|
nullptr>
|
||||||
|
constexpr explicit expected_storage_base(unexpect_t, Args &&... args)
|
||||||
|
: m_unexpect(std::forward<Args>(args)...), m_has_val(false) {}
|
||||||
|
|
||||||
template <class... Args, detail::enable_if_t<std::is_constructible<E, Args&&...>::value>* = nullptr>
|
template <class U, class... Args,
|
||||||
constexpr explicit expected_storage_base(unexpect_t, Args&&... args) :
|
detail::enable_if_t<std::is_constructible<
|
||||||
m_unexpect(std::forward<Args>(args)...), m_has_val(false) {}
|
E, std::initializer_list<U> &, Args &&...>::value> * = nullptr>
|
||||||
|
constexpr explicit expected_storage_base(unexpect_t,
|
||||||
template <class U, class... Args, detail::enable_if_t<std::is_constructible<E, std::initializer_list<U>&, Args&&...>::value>* = nullptr>
|
std::initializer_list<U> il,
|
||||||
constexpr explicit expected_storage_base(unexpect_t, std::initializer_list<U> il, Args&&... args) :
|
Args &&... args)
|
||||||
m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {}
|
: m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {}
|
||||||
|
|
||||||
|
|
||||||
~expected_storage_base() = default;
|
~expected_storage_base() = default;
|
||||||
|
|
||||||
bool m_has_val;
|
bool m_has_val;
|
||||||
struct dummy{};
|
struct dummy {};
|
||||||
union {
|
union {
|
||||||
dummy m_val;
|
dummy m_val;
|
||||||
unexpected<E> m_unexpect;
|
unexpected<E> m_unexpect;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class E>
|
template <class E> struct expected_storage_base<void, E, false, false> {
|
||||||
struct expected_storage_base<void,E,false,false> {
|
constexpr expected_storage_base() : m_val(), m_has_val(true) {}
|
||||||
constexpr expected_storage_base()
|
|
||||||
: m_val(), m_has_val(true) {}
|
|
||||||
|
|
||||||
template <class... Args, detail::enable_if_t<std::is_constructible<E, Args&&...>::value>* = nullptr>
|
template <class... Args,
|
||||||
constexpr explicit expected_storage_base(unexpect_t, Args&&... args) :
|
detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * =
|
||||||
m_unexpect(std::forward<Args>(args)...), m_has_val(false) {}
|
nullptr>
|
||||||
|
constexpr explicit expected_storage_base(unexpect_t, Args &&... args)
|
||||||
template <class U, class... Args, detail::enable_if_t<std::is_constructible<E, std::initializer_list<U>&, Args&&...>::value>* = nullptr>
|
: m_unexpect(std::forward<Args>(args)...), m_has_val(false) {}
|
||||||
constexpr explicit expected_storage_base(unexpect_t, std::initializer_list<U> il, Args&&... args) :
|
|
||||||
m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {}
|
|
||||||
|
|
||||||
|
template <class U, class... Args,
|
||||||
|
detail::enable_if_t<std::is_constructible<
|
||||||
|
E, std::initializer_list<U> &, Args &&...>::value> * = nullptr>
|
||||||
|
constexpr explicit expected_storage_base(unexpect_t,
|
||||||
|
std::initializer_list<U> il,
|
||||||
|
Args &&... args)
|
||||||
|
: m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {}
|
||||||
|
|
||||||
~expected_storage_base() {
|
~expected_storage_base() {
|
||||||
if (m_has_val) {
|
if (m_has_val) {
|
||||||
@@ -272,54 +298,55 @@ namespace detail {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool m_has_val;
|
bool m_has_val;
|
||||||
struct dummy{};
|
struct dummy {};
|
||||||
union {
|
union {
|
||||||
dummy m_val;
|
dummy m_val;
|
||||||
unexpected<E> m_unexpect;
|
unexpected<E> m_unexpect;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
//TODO, conditionally delete things
|
// TODO, conditionally delete things
|
||||||
template <class T, class E>
|
template <class T, class E> class expected_ctor_base {};
|
||||||
class expected_ctor_base{};
|
} // namespace detail
|
||||||
}
|
|
||||||
|
|
||||||
|
template <class E> class bad_expected_access : public std::exception {
|
||||||
template <class E>
|
|
||||||
class bad_expected_access : public std::exception {
|
|
||||||
public:
|
public:
|
||||||
explicit bad_expected_access(E e)
|
explicit bad_expected_access(E e) : m_val(std::move(e)) {}
|
||||||
: m_val(std::move(e)) {}
|
|
||||||
|
|
||||||
virtual const char* what() const noexcept override {
|
virtual const char *what() const noexcept override {
|
||||||
return "Bad expected access";
|
return "Bad expected access";
|
||||||
}
|
}
|
||||||
|
|
||||||
const E& error() const& { return m_val; }
|
const E &error() const & { return m_val; }
|
||||||
E& error() & { return m_val; }
|
E &error() & { return m_val; }
|
||||||
const E&& error() const && { return std::move(m_val); }
|
const E &&error() const && { return std::move(m_val); }
|
||||||
E&& error() && { return std::move(m_val); }
|
E &&error() && { return std::move(m_val); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
E m_val;
|
E m_val;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <class T, class E>
|
template <class T, class E>
|
||||||
class expected : private detail::expected_storage_base<T,E>, private detail::expected_ctor_base<T,E> {
|
class expected : private detail::expected_storage_base<T, E>,
|
||||||
|
private detail::expected_ctor_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, "T must not be in_place_t");
|
static_assert(!std::is_same<T, std::remove_cv<in_place_t>>::value,
|
||||||
static_assert(!std::is_same<T, std::remove_cv<unexpect_t>>::value, "T must not be unexpect_t");
|
"T must not be in_place_t");
|
||||||
static_assert(!std::is_same<T, std::remove_cv<unexpected<E>>>::value, "T must not be unexpected<E>");
|
static_assert(!std::is_same<T, std::remove_cv<unexpect_t>>::value,
|
||||||
|
"T must not be unexpect_t");
|
||||||
|
static_assert(!std::is_same<T, std::remove_cv<unexpected<E>>>::value,
|
||||||
|
"T must not be unexpected<E>");
|
||||||
static_assert(!std::is_reference<E>::value, "E must not be a reference");
|
static_assert(!std::is_reference<E>::value, "E must not be a reference");
|
||||||
static_assert(!std::is_same<T, void>::value, "T must not be void");
|
static_assert(!std::is_same<T, void>::value, "T must not be void");
|
||||||
|
|
||||||
T* valptr() { return std::addressof(this->m_val); }
|
T *valptr() { return std::addressof(this->m_val); }
|
||||||
unexpected<E>* errptr() { return std::addressof(this->m_unexpect); }
|
unexpected<E> *errptr() { return std::addressof(this->m_unexpect); }
|
||||||
T& val() { return this->m_val; }
|
T &val() { return this->m_val; }
|
||||||
unexpected<E>& err() { return this->m_unexpect; }
|
unexpected<E> &err() { return this->m_unexpect; }
|
||||||
const T& val() const { return this->m_val; }
|
const T &val() const { return this->m_val; }
|
||||||
const unexpected<E>& err() const { return this->m_unexpect; }
|
const unexpected<E> &err() const { return this->m_unexpect; }
|
||||||
|
|
||||||
using storage_base = detail::expected_storage_base<T,E>;
|
using storage_base = detail::expected_storage_base<T, E>;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef T value_type;
|
typedef T value_type;
|
||||||
@@ -328,243 +355,271 @@ public:
|
|||||||
|
|
||||||
constexpr expected() = default;
|
constexpr expected() = default;
|
||||||
|
|
||||||
template <class... Args, detail::enable_if_t<std::is_constructible<T, Args&&...>::value>* = nullptr>
|
template <class... Args,
|
||||||
constexpr expected(in_place_t, Args&&... args) :
|
detail::enable_if_t<std::is_constructible<T, Args &&...>::value> * =
|
||||||
storage_base(in_place, std::forward<Args>(args)...){}
|
nullptr>
|
||||||
|
constexpr expected(in_place_t, Args &&... args)
|
||||||
|
: storage_base(in_place, std::forward<Args>(args)...) {}
|
||||||
|
|
||||||
template <class U, class... Args, detail::enable_if_t<std::is_constructible<T, std::initializer_list<U>&, Args&&...>::value>* = nullptr>
|
template <class U, class... Args,
|
||||||
constexpr expected(in_place_t, std::initializer_list<U> il, Args&&... args) :
|
detail::enable_if_t<std::is_constructible<
|
||||||
storage_base(in_place, il, std::forward<Args>(args)...){}
|
T, std::initializer_list<U> &, Args &&...>::value> * = nullptr>
|
||||||
|
constexpr expected(in_place_t, std::initializer_list<U> il, Args &&... args)
|
||||||
|
: storage_base(in_place, il, std::forward<Args>(args)...) {}
|
||||||
|
|
||||||
template <class G = E, detail::enable_if_t<std::is_constructible<E, const G&>::value>* = nullptr, detail::enable_if_t<!std::is_convertible<const G&, E>::value>* = nullptr>
|
template <class G = E,
|
||||||
explicit constexpr expected(unexpected<G> const& e)
|
detail::enable_if_t<std::is_constructible<E, const G &>::value> * =
|
||||||
|
nullptr,
|
||||||
|
detail::enable_if_t<!std::is_convertible<const G &, E>::value> * =
|
||||||
|
nullptr>
|
||||||
|
explicit constexpr expected(unexpected<G> const &e)
|
||||||
: storage_base(unexpect, e) {}
|
: storage_base(unexpect, e) {}
|
||||||
|
|
||||||
template <class G = E, detail::enable_if_t<std::is_constructible<E, const G&>::value>* = nullptr, detail::enable_if_t<std::is_convertible<const G&, E>::value>* = nullptr>
|
template <
|
||||||
constexpr expected(unexpected<G> const& e)
|
class G = E,
|
||||||
: storage_base(unexpect, e) {}
|
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) {}
|
||||||
|
|
||||||
template <class G = E, detail::enable_if_t<std::is_constructible<E, G&&>::value>* = nullptr, detail::enable_if_t<!std::is_convertible<G&&, E>::value>* = nullptr>
|
template <
|
||||||
explicit constexpr expected(unexpected<G> && e) noexcept(std::is_nothrow_constructible<E, G&&>::value)
|
class G = E,
|
||||||
|
detail::enable_if_t<std::is_constructible<E, G &&>::value> * = nullptr,
|
||||||
|
detail::enable_if_t<!std::is_convertible<G &&, E>::value> * = nullptr>
|
||||||
|
explicit constexpr expected(unexpected<G> &&e) noexcept(
|
||||||
|
std::is_nothrow_constructible<E, G &&>::value)
|
||||||
: storage_base(unexpect, std::move(e)) {}
|
: storage_base(unexpect, std::move(e)) {}
|
||||||
|
|
||||||
template <class G = E, detail::enable_if_t<std::is_constructible<E, G&&>::value>* = nullptr, detail::enable_if_t<std::is_convertible<G&&, E>::value>* = nullptr>
|
template <
|
||||||
constexpr expected(unexpected<G> && e) noexcept(std::is_nothrow_constructible<E, G&&>::value)
|
class G = E,
|
||||||
|
detail::enable_if_t<std::is_constructible<E, G &&>::value> * = nullptr,
|
||||||
|
detail::enable_if_t<std::is_convertible<G &&, E>::value> * = nullptr>
|
||||||
|
constexpr expected(unexpected<G> &&e) noexcept(
|
||||||
|
std::is_nothrow_constructible<E, G &&>::value)
|
||||||
: storage_base(unexpect, std::move(e)) {}
|
: storage_base(unexpect, std::move(e)) {}
|
||||||
|
|
||||||
template <class... Args, detail::enable_if_t<std::is_constructible<E, Args&&...>::value>* = nullptr>
|
template <class... Args,
|
||||||
constexpr explicit expected(unexpect_t, Args&&... args)
|
detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * =
|
||||||
|
nullptr>
|
||||||
|
constexpr explicit expected(unexpect_t, Args &&... args)
|
||||||
: storage_base(unexpect, std::forward<Args>(args)...) {}
|
: storage_base(unexpect, std::forward<Args>(args)...) {}
|
||||||
|
|
||||||
template <class U, class... Args, detail::enable_if_t<std::is_constructible<E, std::initializer_list<U>&, Args&&...>::value>* = nullptr>
|
template <class U, class... Args,
|
||||||
constexpr explicit expected(unexpect_t, std::initializer_list<U> il, Args&&... args)
|
detail::enable_if_t<std::is_constructible<
|
||||||
|
E, std::initializer_list<U> &, Args &&...>::value> * = nullptr>
|
||||||
|
constexpr explicit expected(unexpect_t, std::initializer_list<U> il,
|
||||||
|
Args &&... args)
|
||||||
: storage_base(unexpect, il, std::forward<Args>(args)...) {}
|
: storage_base(unexpect, il, std::forward<Args>(args)...) {}
|
||||||
|
|
||||||
constexpr expected(const expected& rhs) {
|
constexpr expected(const expected &rhs) {
|
||||||
if (rhs.has_value()) {
|
if (rhs.has_value()) {
|
||||||
::new (valptr()) T (*rhs);
|
::new (valptr()) T(*rhs);
|
||||||
}
|
} else {
|
||||||
else {
|
::new (errptr()) unexpected_type(unexpected<E>(rhs.error()));
|
||||||
::new (errptr()) unexpected_type (unexpected<E>(rhs.error()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO SFINAE
|
// TODO SFINAE
|
||||||
constexpr expected(expected&& rhs) noexcept(std::is_nothrow_move_constructible<T>::value && std::is_nothrow_move_constructible<E>::value) {
|
constexpr expected(expected &&rhs) noexcept(
|
||||||
|
std::is_nothrow_move_constructible<T>::value
|
||||||
|
&&std::is_nothrow_move_constructible<E>::value) {
|
||||||
if (rhs.has_value()) {
|
if (rhs.has_value()) {
|
||||||
::new (valptr()) T (std::move(*rhs));
|
::new (valptr()) T(std::move(*rhs));
|
||||||
}
|
} else {
|
||||||
else {
|
::new (errptr()) unexpected_type(unexpected<E>(std::move(rhs.error())));
|
||||||
::new (errptr()) unexpected_type (unexpected<E>(std::move(rhs.error())));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO SFINAE
|
// TODO SFINAE
|
||||||
template <class U, class G, detail::enable_if_t<(!std::is_convertible<U const&, T>::value || !std::is_convertible<G const&, E>::value)>* = nullptr>
|
template <class U, class G,
|
||||||
explicit constexpr expected(const expected<U, G>& rhs) {
|
detail::enable_if_t<(!std::is_convertible<U const &, T>::value ||
|
||||||
|
!std::is_convertible<G const &, E>::value)> * =
|
||||||
|
nullptr>
|
||||||
|
explicit constexpr expected(const expected<U, G> &rhs) {
|
||||||
if (rhs.has_value()) {
|
if (rhs.has_value()) {
|
||||||
::new (valptr()) T (*rhs);
|
::new (valptr()) T(*rhs);
|
||||||
}
|
} else {
|
||||||
else {
|
::new (errptr()) unexpected_type(unexpected<E>(rhs.error()));
|
||||||
::new (errptr()) unexpected_type (unexpected<E>(rhs.error()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO SFINAE
|
// TODO SFINAE
|
||||||
template <class U, class G, detail::enable_if_t<(!std::is_convertible<U&&, T>::value || !std::is_convertible<G&&, E>::value)>* = nullptr>
|
template <
|
||||||
explicit constexpr expected(const expected<U, G>& rhs) {
|
class U, class G,
|
||||||
|
detail::enable_if_t<(!std::is_convertible<U &&, T>::value ||
|
||||||
|
!std::is_convertible<G &&, E>::value)> * = nullptr>
|
||||||
|
explicit constexpr expected(const expected<U, G> &rhs) {
|
||||||
if (rhs.has_value()) {
|
if (rhs.has_value()) {
|
||||||
::new (valptr()) T (*rhs);
|
::new (valptr()) T(*rhs);
|
||||||
}
|
} else {
|
||||||
else {
|
::new (errptr()) unexpected_type(unexpected<E>(rhs.error()));
|
||||||
::new (errptr()) unexpected_type (unexpected<E>(rhs.error()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO SFINAE
|
// TODO SFINAE
|
||||||
template <class U, class G, detail::enable_if_t<(std::is_convertible<U&&, T>::value || std::is_convertible<G&&, E>::value)>* = nullptr>
|
template <
|
||||||
constexpr expected(expected<U, G>&& rhs) {
|
class U, class G,
|
||||||
|
detail::enable_if_t<(std::is_convertible<U &&, T>::value ||
|
||||||
|
std::is_convertible<G &&, E>::value)> * = nullptr>
|
||||||
|
constexpr expected(expected<U, G> &&rhs) {
|
||||||
if (rhs.has_value()) {
|
if (rhs.has_value()) {
|
||||||
::new (valptr()) T (std::move(*rhs));
|
::new (valptr()) T(std::move(*rhs));
|
||||||
}
|
} else {
|
||||||
else {
|
::new (errptr()) unexpected_type(unexpected<E>(std::move(rhs.error())));
|
||||||
::new (errptr()) unexpected_type (unexpected<E>(std::move(rhs.error())));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO SFINAE
|
// TODO SFINAE
|
||||||
template <class U = T, detail::enable_if_t<!std::is_convertible<U&&, T>::value>* = nullptr>
|
template <class U = T, detail::enable_if_t<
|
||||||
explicit constexpr expected(U&& v)
|
!std::is_convertible<U &&, T>::value> * = nullptr>
|
||||||
: expected(in_place, std::forward<U>(v))
|
explicit constexpr expected(U &&v) : expected(in_place, std::forward<U>(v)) {}
|
||||||
{}
|
|
||||||
|
|
||||||
//TODO SFINAE
|
// TODO SFINAE
|
||||||
template <class U = T, detail::enable_if_t<std::is_convertible<U&&, T>::value>* = nullptr>
|
template <class U = T, detail::enable_if_t<
|
||||||
constexpr expected(U&& v)
|
std::is_convertible<U &&, T>::value> * = nullptr>
|
||||||
: expected(in_place, std::forward<U>(v))
|
constexpr expected(U &&v) : expected(in_place, std::forward<U>(v)) {}
|
||||||
{}
|
|
||||||
|
|
||||||
//TODO
|
// TODO
|
||||||
expected& operator=(const expected&);
|
expected &operator=(const expected &);
|
||||||
expected& operator=(expected&&) noexcept;
|
expected &operator=(expected &&) noexcept;
|
||||||
template <class U = T> expected& operator=(U&&);
|
template <class U = T> expected &operator=(U &&);
|
||||||
template <class G = E>
|
template <class G = E> expected &operator=(const unexpected<G> &);
|
||||||
expected& operator=(const unexpected<G>&);
|
template <class G = E> expected &operator=(unexpected<G> &&) noexcept;
|
||||||
template <class G = E>
|
template <class... Args> void emplace(Args &&...);
|
||||||
expected& operator=(unexpected<G>&&) noexcept;
|
|
||||||
template <class... Args>
|
|
||||||
void emplace(Args&&...);
|
|
||||||
template <class U, class... Args>
|
template <class U, class... Args>
|
||||||
void emplace(std::initializer_list<U>, Args&&...);
|
void emplace(std::initializer_list<U>, Args &&...);
|
||||||
|
|
||||||
//TODO SFINAE
|
// TODO SFINAE
|
||||||
void swap(expected& rhs) noexcept(std::is_nothrow_move_constructible<T>::value && noexcept(swap(std::declval<T&>(), std::declval<T&>())) && std::is_nothrow_move_constructible<E>::value && noexcept(swap(std::declval<E&>(), std::declval<E&>())))
|
void swap(expected &rhs) noexcept(
|
||||||
{
|
std::is_nothrow_move_constructible<T>::value &&noexcept(
|
||||||
|
swap(std::declval<T &>(), std::declval<T &>())) &&
|
||||||
|
std::is_nothrow_move_constructible<E>::value &&
|
||||||
|
noexcept(swap(std::declval<E &>(), std::declval<E &>()))) {
|
||||||
if (has_value() && rhs.has_value()) {
|
if (has_value() && rhs.has_value()) {
|
||||||
using std::swap;
|
using std::swap;
|
||||||
swap(val(), rhs.val());
|
swap(val(), rhs.val());
|
||||||
}
|
} else if (!has_value() && rhs.has_value()) {
|
||||||
else if (!has_value() && rhs.has_value()) {
|
|
||||||
using std::swap;
|
using std::swap;
|
||||||
swap(err(), rhs.err());
|
swap(err(), rhs.err());
|
||||||
}
|
} else if (has_value()) {
|
||||||
else if (has_value()) {
|
|
||||||
auto temp = std::move(rhs.err());
|
auto temp = std::move(rhs.err());
|
||||||
::new (rhs.valptr()) T (val());
|
::new (rhs.valptr()) T(val());
|
||||||
::new (errptr()) unexpected_type (std::move(temp));
|
::new (errptr()) unexpected_type(std::move(temp));
|
||||||
std::swap(this->m_has_val, rhs.m_has_val);
|
std::swap(this->m_has_val, rhs.m_has_val);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
auto temp = std::move(this->err());
|
auto temp = std::move(this->err());
|
||||||
::new (valptr()) T (rhs.val());
|
::new (valptr()) T(rhs.val());
|
||||||
::new (errptr()) unexpected_type (std::move(temp));
|
::new (errptr()) unexpected_type(std::move(temp));
|
||||||
std::swap(this->m_has_val, rhs.m_has_val);
|
std::swap(this->m_has_val, rhs.m_has_val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr const T* operator ->() const { return valptr(); }
|
constexpr const T *operator->() const { return valptr(); }
|
||||||
constexpr T* operator ->() { return valptr(); }
|
constexpr T *operator->() { return valptr(); }
|
||||||
constexpr const T& operator *() const& { return val(); }
|
constexpr const T &operator*() const & { return val(); }
|
||||||
constexpr T& operator *() & { return val(); }
|
constexpr T &operator*() & { return val(); }
|
||||||
constexpr const T&& operator *() const && { return std::move(val()); }
|
constexpr const T &&operator*() const && { return std::move(val()); }
|
||||||
constexpr T&& operator *() && { return std::move(val()); }
|
constexpr T &&operator*() && { return std::move(val()); }
|
||||||
constexpr explicit operator bool() const noexcept { return this->m_has_val; }
|
constexpr explicit operator bool() const noexcept { return this->m_has_val; }
|
||||||
constexpr bool has_value() const noexcept { return this->m_has_val; }
|
constexpr bool has_value() const noexcept { return this->m_has_val; }
|
||||||
constexpr const T& value() const& {
|
constexpr const T &value() const & {
|
||||||
if (!has_value()) throw bad_expected_access<E>(err());
|
if (!has_value())
|
||||||
|
throw bad_expected_access<E>(err());
|
||||||
return val();
|
return val();
|
||||||
}
|
}
|
||||||
constexpr T& value() & {
|
constexpr T &value() & {
|
||||||
if (!has_value()) throw bad_expected_access<E>(err());
|
if (!has_value())
|
||||||
|
throw bad_expected_access<E>(err());
|
||||||
return val();
|
return val();
|
||||||
}
|
}
|
||||||
constexpr const T&& value() const && {
|
constexpr const T &&value() const && {
|
||||||
if (!has_value()) throw bad_expected_access<E>(err());
|
if (!has_value())
|
||||||
|
throw bad_expected_access<E>(err());
|
||||||
return std::move(val());
|
return std::move(val());
|
||||||
}
|
}
|
||||||
constexpr T&& value() && {
|
constexpr T &&value() && {
|
||||||
if (!has_value()) throw bad_expected_access<E>(err());
|
if (!has_value())
|
||||||
|
throw bad_expected_access<E>(err());
|
||||||
return std::move(val());
|
return std::move(val());
|
||||||
}
|
}
|
||||||
constexpr const E& error() const& { return err().value(); }
|
constexpr const E &error() const & { return err().value(); }
|
||||||
constexpr E& error() & { return err().value(); }
|
constexpr E &error() & { return err().value(); }
|
||||||
constexpr const E&& error() const && { return std::move(err().value()); }
|
constexpr const E &&error() const && { return std::move(err().value()); }
|
||||||
constexpr E&& error() && { return std::move(err().value()); }
|
constexpr E &&error() && { return std::move(err().value()); }
|
||||||
template <class U>
|
template <class U> constexpr T value_or(U &&v) const & {
|
||||||
constexpr T value_or(U&& v) const& {
|
static_assert(std::is_copy_constructible<T>::value &&
|
||||||
static_assert(std::is_copy_constructible<T>::value && std::is_convertible<U&&, T>::value, "T must be copy-constructible and convertible to from U&&");
|
std::is_convertible<U &&, T>::value,
|
||||||
|
"T must be copy-constructible and convertible to from U&&");
|
||||||
return bool(*this) ? **this : static_cast<T>(std::forward<U>(v));
|
return bool(*this) ? **this : static_cast<T>(std::forward<U>(v));
|
||||||
}
|
}
|
||||||
template <class U>
|
template <class U> T value_or(U &&v) && {
|
||||||
T value_or(U&& v) && {
|
static_assert(std::is_move_constructible<T>::value &&
|
||||||
static_assert(std::is_move_constructible<T>::value && std::is_convertible<U&&, T>::value, "T must be move-constructible and convertible to from U&&");
|
std::is_convertible<U &&, T>::value,
|
||||||
|
"T must be move-constructible and convertible to from U&&");
|
||||||
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));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
template <class E> class expected<void, E> {};
|
||||||
|
|
||||||
//TODO
|
|
||||||
template <class E>
|
|
||||||
class expected<void, E> {
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
template <class T, class E>
|
|
||||||
constexpr bool operator==(const expected<T, E>& lhs, const expected<T, E>& rhs) {
|
|
||||||
return (lhs.has_value() != rhs.has_value()) ? false :
|
|
||||||
(!lhs.has_value() ? lhs.error() == rhs.error()
|
|
||||||
: *lhs == *rhs);
|
|
||||||
}
|
|
||||||
template <class T, class E>
|
template <class T, class E>
|
||||||
constexpr bool operator!=(const expected<T, E>& lhs, const expected<T, E>& rhs) {
|
constexpr bool operator==(const expected<T, E> &lhs,
|
||||||
return (lhs.has_value() != rhs.has_value()) ? true :
|
const expected<T, E> &rhs) {
|
||||||
(!lhs.has_value() ? lhs.error() != rhs.error()
|
return (lhs.has_value() != rhs.has_value())
|
||||||
: *lhs != *rhs);
|
? false
|
||||||
|
: (!lhs.has_value() ? lhs.error() == rhs.error() : *lhs == *rhs);
|
||||||
|
}
|
||||||
|
template <class T, class E>
|
||||||
|
constexpr bool operator!=(const expected<T, E> &lhs,
|
||||||
|
const expected<T, E> &rhs) {
|
||||||
|
return (lhs.has_value() != rhs.has_value())
|
||||||
|
? true
|
||||||
|
: (!lhs.has_value() ? lhs.error() != rhs.error() : *lhs != *rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T, class E>
|
template <class T, class E>
|
||||||
constexpr bool operator==(const expected<T, E>& x, const T& v) {
|
constexpr bool operator==(const expected<T, E> &x, const T &v) {
|
||||||
return x.has_value() ? *x == v : false;
|
return x.has_value() ? *x == v : false;
|
||||||
}
|
}
|
||||||
template <class T, class E>
|
template <class T, class E>
|
||||||
constexpr bool operator==(const T& v, const expected<T, E>& x) {
|
constexpr bool operator==(const T &v, const expected<T, E> &x) {
|
||||||
return x.has_value() ? *x == v : false;
|
return x.has_value() ? *x == v : false;
|
||||||
}
|
}
|
||||||
template <class T, class E>
|
template <class T, class E>
|
||||||
constexpr bool operator!=(const expected<T, E>& x, const T& v) {
|
constexpr bool operator!=(const expected<T, E> &x, const T &v) {
|
||||||
return x.has_value() ? *x != v : true;
|
return x.has_value() ? *x != v : true;
|
||||||
}
|
}
|
||||||
template <class T, class E>
|
template <class T, class E>
|
||||||
constexpr bool operator!=(const T& v, const expected<T, E>& x) {
|
constexpr bool operator!=(const T &v, const expected<T, E> &x) {
|
||||||
return x.has_value() ? *x != v : true;
|
return x.has_value() ? *x != v : true;
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
template <class T, class E>
|
template <class T, class E>
|
||||||
constexpr bool operator==(const unexpected<E>& e, const expected<T, E>& x) {
|
constexpr bool operator==(const unexpected<E> &e, const expected<T, E> &x) {
|
||||||
return x.has_value() ? true : x.error() == e.value();
|
return x.has_value() ? true : x.error() == e.value();
|
||||||
}
|
}
|
||||||
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() ? false : x.error() != e.value();
|
return x.has_value() ? false : x.error() != e.value();
|
||||||
}
|
}
|
||||||
template <class T, class E>
|
template <class T, class E>
|
||||||
constexpr bool operator!=(const unexpected<E>& e, const expected<T, E>& x) {
|
constexpr bool operator!=(const unexpected<E> &e, const expected<T, E> &x) {
|
||||||
return x.has_value() ? false : x.error() != e.value();
|
return x.has_value() ? false : x.error() != e.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO is_swappable
|
||||||
//TODO is_swappable
|
template <class T, class E,
|
||||||
template <class T, class E, detail::enable_if_t<std::is_move_constructible<T>::value && std::is_move_constructible<E>::value>* = nullptr>
|
detail::enable_if_t<std::is_move_constructible<T>::value &&
|
||||||
void swap(expected<T, E>& lhs, expected<T, E>& rhs) noexcept(noexcept(lhs.swap(rhs))) {
|
std::is_move_constructible<E>::value> * = nullptr>
|
||||||
|
void swap(expected<T, E> &lhs,
|
||||||
|
expected<T, E> &rhs) noexcept(noexcept(lhs.swap(rhs))) {
|
||||||
lhs.swap(rhs);
|
lhs.swap(rhs);
|
||||||
}
|
}
|
||||||
}
|
} // namespace tl
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user