Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require in_place_t with variadic unexpected ctors #169

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 43 additions & 40 deletions include/tl/expected.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,21 @@ template <class E> class unexpected {

template <class... Args, typename std::enable_if<std::is_constructible<
E, Args &&...>::value>::type * = nullptr>
constexpr explicit unexpected(Args &&...args)
constexpr explicit unexpected(in_place_t, Args &&...args)
: m_val(std::forward<Args>(args)...) {}

template <
class U, class... Args,
typename std::enable_if<std::is_constructible<
E, std::initializer_list<U> &, Args &&...>::value>::type * = nullptr>
constexpr explicit unexpected(std::initializer_list<U> l, Args &&...args)
constexpr explicit unexpected(in_place_t, std::initializer_list<U> l,
Args &&...args)
: m_val(l, std::forward<Args>(args)...) {}

constexpr const E &value() const & { return m_val; }
TL_EXPECTED_11_CONSTEXPR E &value() & { return m_val; }
TL_EXPECTED_11_CONSTEXPR E &&value() && { return std::move(m_val); }
constexpr const E &&value() const && { return std::move(m_val); }
constexpr const E &error() const & { return m_val; }
TL_EXPECTED_11_CONSTEXPR E &error() & { return m_val; }
TL_EXPECTED_11_CONSTEXPR E &&error() && { return std::move(m_val); }
constexpr const E &&error() const && { return std::move(m_val); }

private:
E m_val;
Expand All @@ -180,27 +182,27 @@ template <class E> unexpected(E) -> unexpected<E>;

template <class E>
constexpr bool operator==(const unexpected<E> &lhs, const unexpected<E> &rhs) {
return lhs.value() == rhs.value();
return lhs.error() == rhs.error();
}
template <class E>
constexpr bool operator!=(const unexpected<E> &lhs, const unexpected<E> &rhs) {
return lhs.value() != rhs.value();
return lhs.error() != rhs.error();
}
template <class E>
constexpr bool operator<(const unexpected<E> &lhs, const unexpected<E> &rhs) {
return lhs.value() < rhs.value();
return lhs.error() < rhs.error();
}
template <class E>
constexpr bool operator<=(const unexpected<E> &lhs, const unexpected<E> &rhs) {
return lhs.value() <= rhs.value();
return lhs.error() <= rhs.error();
}
template <class E>
constexpr bool operator>(const unexpected<E> &lhs, const unexpected<E> &rhs) {
return lhs.value() > rhs.value();
return lhs.error() > rhs.error();
}
template <class E>
constexpr bool operator>=(const unexpected<E> &lhs, const unexpected<E> &rhs) {
return lhs.value() >= rhs.value();
return lhs.error() >= rhs.error();
}

template <class E>
Expand Down Expand Up @@ -471,15 +473,15 @@ struct expected_storage_base {
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) {}
: m_unexpect(in_place, 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) {}
: m_unexpect(in_place, il, std::forward<Args>(args)...), m_has_val(false) {}

~expected_storage_base() {
if (m_has_val) {
Expand Down Expand Up @@ -518,15 +520,15 @@ template <class T, class E> struct expected_storage_base<T, E, true, true> {
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) {}
: m_unexpect(in_place, 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) {}
: m_unexpect(in_place, il, std::forward<Args>(args)...), m_has_val(false) {}

expected_storage_base(const expected_storage_base &) = default;
expected_storage_base(expected_storage_base &&) = default;
Expand Down Expand Up @@ -563,15 +565,15 @@ template <class T, class E> struct expected_storage_base<T, E, true, false> {
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) {}
: m_unexpect(in_place, 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) {}
: m_unexpect(in_place, il, std::forward<Args>(args)...), m_has_val(false) {}

expected_storage_base(const expected_storage_base &) = default;
expected_storage_base(expected_storage_base &&) = default;
Expand Down Expand Up @@ -612,15 +614,16 @@ template <class T, class E> struct expected_storage_base<T, E, false, true> {
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) {}
: m_unexpect(in_place, 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) {}
: m_unexpect(in_place, il, std::forward<Args>(args)...),
m_has_val(false) {}

expected_storage_base(const expected_storage_base &) = default;
expected_storage_base(expected_storage_base &&) = default;
Expand Down Expand Up @@ -656,15 +659,15 @@ template <class E> struct expected_storage_base<void, E, false, true> {
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) {}
: m_unexpect(in_place, 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) {}
: m_unexpect(in_place, il, std::forward<Args>(args)...), m_has_val(false) {}

expected_storage_base(const expected_storage_base &) = default;
expected_storage_base(expected_storage_base &&) = default;
Expand All @@ -690,15 +693,15 @@ template <class E> struct expected_storage_base<void, E, false, false> {
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) {}
: m_unexpect(in_place, 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) {}
: m_unexpect(in_place, il, std::forward<Args>(args)...), m_has_val(false) {}

expected_storage_base(const expected_storage_base &) = default;
expected_storage_base(expected_storage_base &&) = default;
Expand Down Expand Up @@ -1582,7 +1585,7 @@ class expected : private detail::expected_move_assign_base<T, E>,
detail::enable_if_t<!std::is_convertible<const G &, E>::value> * =
nullptr>
explicit constexpr expected(const unexpected<G> &e)
: impl_base(unexpect, e.value()),
: impl_base(unexpect, e.error()),
ctor_base(detail::default_constructor_tag{}) {}

template <
Expand All @@ -1591,7 +1594,7 @@ class expected : private detail::expected_move_assign_base<T, E>,
nullptr,
detail::enable_if_t<std::is_convertible<const G &, E>::value> * = nullptr>
constexpr expected(unexpected<G> const &e)
: impl_base(unexpect, e.value()),
: impl_base(unexpect, e.error()),
ctor_base(detail::default_constructor_tag{}) {}

template <
Expand All @@ -1600,7 +1603,7 @@ class expected : private detail::expected_move_assign_base<T, E>,
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)
: impl_base(unexpect, std::move(e.value())),
: impl_base(unexpect, std::move(e.error())),
ctor_base(detail::default_constructor_tag{}) {}

template <
Expand All @@ -1609,7 +1612,7 @@ class expected : private detail::expected_move_assign_base<T, E>,
detail::enable_if_t<std::is_convertible<G &&, E>::value> * = nullptr>
constexpr expected(unexpected<G> &&e) noexcept(
std::is_nothrow_constructible<E, G &&>::value)
: impl_base(unexpect, std::move(e.value())),
: impl_base(unexpect, std::move(e.error())),
ctor_base(detail::default_constructor_tag{}) {}

template <class... Args,
Expand Down Expand Up @@ -2017,46 +2020,46 @@ class expected : private detail::expected_move_assign_base<T, E>,
detail::enable_if_t<!std::is_void<U>::value> * = nullptr>
TL_EXPECTED_11_CONSTEXPR const U &value() const & {
if (!has_value())
detail::throw_exception(bad_expected_access<E>(err().value()));
detail::throw_exception(bad_expected_access<E>(err().error()));
return val();
}
template <class U = T,
detail::enable_if_t<!std::is_void<U>::value> * = nullptr>
TL_EXPECTED_11_CONSTEXPR U &value() & {
if (!has_value())
detail::throw_exception(bad_expected_access<E>(err().value()));
detail::throw_exception(bad_expected_access<E>(err().error()));
return val();
}
template <class U = T,
detail::enable_if_t<!std::is_void<U>::value> * = nullptr>
TL_EXPECTED_11_CONSTEXPR const U &&value() const && {
if (!has_value())
detail::throw_exception(bad_expected_access<E>(std::move(err()).value()));
detail::throw_exception(bad_expected_access<E>(std::move(err()).error()));
return std::move(val());
}
template <class U = T,
detail::enable_if_t<!std::is_void<U>::value> * = nullptr>
TL_EXPECTED_11_CONSTEXPR U &&value() && {
if (!has_value())
detail::throw_exception(bad_expected_access<E>(std::move(err()).value()));
detail::throw_exception(bad_expected_access<E>(std::move(err()).error()));
return std::move(val());
}

constexpr const E &error() const & {
TL_ASSERT(!has_value());
return err().value();
return err().error();
}
TL_EXPECTED_11_CONSTEXPR E &error() & {
TL_ASSERT(!has_value());
return err().value();
return err().error();
}
constexpr const E &&error() const && {
TL_ASSERT(!has_value());
return std::move(err().value());
return std::move(err().error());
}
TL_EXPECTED_11_CONSTEXPR E &&error() && {
TL_ASSERT(!has_value());
return std::move(err().value());
return std::move(err().error());
}

template <class U> constexpr T value_or(U &&v) const & {
Expand Down Expand Up @@ -2446,19 +2449,19 @@ constexpr bool operator!=(const U &v, const expected<T, E> &x) {

template <class T, class 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.error();
}
template <class T, class E>
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.error();
}
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();
return x.has_value() ? true : x.error() != e.error();
}
template <class T, class E>
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.error();
}

template <class T, class E,
Expand Down
Loading