///
// expected - An implementation of std::expected with extensions
// Written in 2017 by Simon Brand (@TartanLlama)
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to the
// public domain worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software. If not, see
// .
///
#ifndef TL_EXPECTED_HPP
#define TL_EXPECTED_HPP
#include
#include
#include
#include
#include
#if (defined(_MSC_VER) && _MSC_VER == 1900)
#define TL_EXPECTED_MSVC2015
#endif
#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9)
#define TL_EXPECTED_GCC49
#endif
#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 4)
#define TL_EXPECTED_GCC54
#endif
#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9)
#define TL_EXPECTED_NO_CONSTRR
#endif
#if __cplusplus > 201103L
#define TL_EXPECTED_CXX14
#endif
#if (__cplusplus == 201103L || defined(TL_EXPECTED_MSVC2015) || \
defined(TL_EXPECTED_GCC49)) && \
!defined(TL_EXPECTED_GCC54)
/// \exclude
#define TL_EXPECTED_11_CONSTEXPR
#else
/// \exclude
#define TL_EXPECTED_11_CONSTEXPR constexpr
#endif
namespace tl {
template class expected;
/// \exclude
namespace detail {
template
using enable_if_t = typename std::enable_if::type;
template using decay_t = typename std::decay::type;
// std::conjunction from C++17
template struct conjunction : std::true_type {};
template struct conjunction : B {};
template
struct conjunction
: std::conditional, B>::type {};
// Trait for checking if a type is a tl::expected
template struct is_expected_impl : std::false_type {};
template
struct is_expected_impl> : std::true_type {};
template using is_expected = is_expected_impl>;
// std::invoke from C++17
// https://stackoverflow.com/questions/38288042/c11-14-invoke-workaround
template >{}>,
int = 0>
constexpr auto invoke(Fn &&f, Args &&... args) noexcept(
noexcept(std::mem_fn(f)(std::forward(args)...)))
-> decltype(std::mem_fn(f)(std::forward(args)...)) {
return std::mem_fn(f)(std::forward(args)...);
}
template >{}>>
constexpr auto invoke(Fn &&f, Args &&... args) noexcept(
noexcept(std::forward(f)(std::forward(args)...)))
-> decltype(std::forward(f)(std::forward(args)...)) {
return std::forward(f)(std::forward(args)...);
}
// std::invoke_result from C++17
template struct invoke_result_impl;
template
struct invoke_result_impl<
F, decltype(invoke(std::declval(), std::declval()...), void()),
Us...> {
using type = decltype(invoke(std::declval(), std::declval()...));
};
template
using invoke_result = invoke_result_impl;
template
using invoke_result_t = typename invoke_result::type;
} // namespace detail
#ifndef TL_IN_PLACE_MONOSTATE_DEFINED
#define TL_IN_PLACE_MONOSTATE_DEFINED
/// \brief Used to represent an expected with no data
class monostate {};
/// \brief A tag type to tell expected to construct its value in-place
struct in_place_t {
explicit in_place_t() = default;
};
/// \brief A tag to tell expected to construct its value in-place
static constexpr in_place_t in_place{};
#endif
/// Used as a wrapper to store the unexpected value
template class unexpected {
public:
static_assert(!std::is_same::value, "E must not be void");
unexpected() = delete;
constexpr explicit unexpected(const E &e) : m_val(e) {}
constexpr explicit unexpected(E &&e) : m_val(std::move(e)) {}
/// \returns the contained value
/// \group unexpected_value
constexpr const E &value() const & { return m_val; }
/// \group unexpected_value
constexpr E &value() & { return m_val; }
/// \group unexpected_value
constexpr E &&value() && { return std::move(m_val); }
/// \exclude
constexpr const E &&value() const && { return std::move(m_val); }
private:
E m_val;
};
/// \brief Compares two unexpected objects
/// \details Simply compares lhs.value() to rhs.value()
/// \group unexpected_relop
template
constexpr bool operator==(const unexpected &lhs, const unexpected &rhs) {
return lhs.value() == rhs.value();
}
/// \group unexpected_relop
template
constexpr bool operator!=(const unexpected &lhs, const unexpected &rhs) {
return lhs.value() != rhs.value();
}
/// \group unexpected_relop
template
constexpr bool operator<(const unexpected &lhs, const unexpected &rhs) {
return lhs.value() < rhs.value();
}
/// \group unexpected_relop
template
constexpr bool operator<=(const unexpected &lhs, const unexpected &rhs) {
return lhs.value() <= rhs.value();
}
/// \group unexpected_relop
template
constexpr bool operator>(const unexpected &lhs, const unexpected &rhs) {
return lhs.value() > rhs.value();
}
/// \group unexpected_relop
template
constexpr bool operator>=(const unexpected &lhs, const unexpected &rhs) {
return lhs.value() >= rhs.value();
}
/// Create an `unexpected` from `e`, deducing the return type
///
/// *Example:*
/// auto e1 = tl::make_unexpected(42);
/// unexpected e2 (42); //same semantics
template unexpected make_unexpected(E &&e) {
return unexpected(std::forward(e));
}
/// \brief A tag type to tell expected to construct the unexpected value
struct unexpect_t {
unexpect_t() = default;
};
/// \brief A tag to tell expected to construct the unexpected value
static constexpr unexpect_t unexpect{};
/// \exclude
namespace detail {
// Implements the storage of the values, and ensures that the destructor is
// trivial if it can be.
//
// This specialization is for where neither `T` or `E` is trivially
// destructible, so the destructors must be called on destruction of the
// `expected`
template ::value,
bool = std::is_trivially_destructible::value>
struct expected_storage_base {
constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {}
template ::value> * =
nullptr>
constexpr expected_storage_base(in_place_t, Args &&... args)
: m_val(std::forward(args)...), m_has_val(true) {}
template &, Args &&...>::value> * = nullptr>
constexpr expected_storage_base(in_place_t, std::initializer_list il,
Args &&... args)
: m_val(il, std::forward(args)...), m_has_val(true) {}
template ::value> * =
nullptr>
constexpr explicit expected_storage_base(unexpect_t, Args &&... args)
: m_unexpect(std::forward(args)...), m_has_val(false) {}
template &, Args &&...>::value> * = nullptr>
constexpr explicit expected_storage_base(unexpect_t,
std::initializer_list il,
Args &&... args)
: m_unexpect(il, std::forward(args)...), m_has_val(false) {}
~expected_storage_base() {
if (m_has_val) {
m_val.~T();
} else {
m_unexpect.~unexpected();
}
}
bool m_has_val;
union {
T m_val;
unexpected m_unexpect;
};
};
// This specialization is for when both `T` and `E` are trivially-destructible,
// so the destructor of the `expected` can be trivial.
template struct expected_storage_base {
constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {}
template ::value> * =
nullptr>
constexpr expected_storage_base(in_place_t, Args &&... args)
: m_val(std::forward(args)...), m_has_val(true) {}
template &, Args &&...>::value> * = nullptr>
constexpr expected_storage_base(in_place_t, std::initializer_list il,
Args &&... args)
: m_val(il, std::forward(args)...), m_has_val(true) {}
template ::value> * =
nullptr>
constexpr explicit expected_storage_base(unexpect_t, Args &&... args)
: m_unexpect(std::forward(args)...), m_has_val(false) {}
template &, Args &&...>::value> * = nullptr>
constexpr explicit expected_storage_base(unexpect_t,
std::initializer_list il,
Args &&... args)
: m_unexpect(il, std::forward(args)...), m_has_val(false) {}
~expected_storage_base() = default;
bool m_has_val;
union {
T m_val;
unexpected m_unexpect;
};
};
// T is trivial, E is not.
template struct expected_storage_base {
constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {}
template ::value> * =
nullptr>
constexpr expected_storage_base(in_place_t, Args &&... args)
: m_val(std::forward(args)...), m_has_val(true) {}
template &, Args &&...>::value> * = nullptr>
constexpr expected_storage_base(in_place_t, std::initializer_list il,
Args &&... args)
: m_val(il, std::forward(args)...), m_has_val(true) {}
template ::value> * =
nullptr>
constexpr explicit expected_storage_base(unexpect_t, Args &&... args)
: m_unexpect(std::forward(args)...), m_has_val(false) {}
template &, Args &&...>::value> * = nullptr>
constexpr explicit expected_storage_base(unexpect_t,
std::initializer_list il,
Args &&... args)
: m_unexpect(il, std::forward(args)...), m_has_val(false) {}
~expected_storage_base() {
if (!m_has_val) {
m_unexpect.~unexpected();
}
}
bool m_has_val;
union {
T m_val;
unexpected m_unexpect;
};
};
// E is trivial, T is not.
template struct expected_storage_base {
constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {}
template ::value> * =
nullptr>
constexpr expected_storage_base(in_place_t, Args &&... args)
: m_val(std::forward(args)...), m_has_val(true) {}
template &, Args &&...>::value> * = nullptr>
constexpr expected_storage_base(in_place_t, std::initializer_list il,
Args &&... args)
: m_val(il, std::forward(args)...), m_has_val(true) {}
template ::value> * =
nullptr>
constexpr explicit expected_storage_base(unexpect_t, Args &&... args)
: m_unexpect(std::forward(args)...), m_has_val(false) {}
template &, Args &&...>::value> * = nullptr>
constexpr explicit expected_storage_base(unexpect_t,
std::initializer_list il,
Args &&... args)
: m_unexpect(il, std::forward(args)...), m_has_val(false) {}
~expected_storage_base() {
if (m_has_val) {
m_val.~T();
}
}
bool m_has_val;
union {
T m_val;
unexpected m_unexpect;
};
};
// expected_copy_move_base is used to conditionally delete the move/copy
// constructors/assignment operators depending on the traits of T and E.
// TODO these could be reduced a bit by splitting construction and assignment
// into different bases
template <
class T, class E,
bool EnableCopy = (std::is_copy_constructible::value &&
std::is_copy_constructible::value),
bool EnableMove = (std::is_move_constructible::value &&
std::is_move_constructible::value),
bool EnableCopyAssign = (std::is_copy_assignable::value &&
std::is_copy_assignable::value &&
std::is_copy_constructible::value &&
std::is_copy_constructible::value &&
std::is_nothrow_move_constructible::value),
bool EnableMoveAssign = (std::is_move_constructible::value &&
std::is_move_assignable::value &&
std::is_nothrow_move_constructible::value &&
std::is_nothrow_move_assignable::value)>
struct expected_copy_move_base {
expected_copy_move_base() = default;
~expected_copy_move_base() = default;
expected_copy_move_base(const expected_copy_move_base &) = default;
expected_copy_move_base(expected_copy_move_base &&) noexcept = default;
expected_copy_move_base &operator=(const expected_copy_move_base &) = default;
expected_copy_move_base &
operator=(expected_copy_move_base &&) noexcept = default;
};
template
struct expected_copy_move_base {
expected_copy_move_base() = default;
~expected_copy_move_base() = default;
expected_copy_move_base(const expected_copy_move_base &) = default;
expected_copy_move_base(expected_copy_move_base &&) noexcept = default;
expected_copy_move_base &operator=(const expected_copy_move_base &) = default;
expected_copy_move_base &
operator=(expected_copy_move_base &&) noexcept = delete;
};
template
struct expected_copy_move_base {
expected_copy_move_base() = default;
~expected_copy_move_base() = default;
expected_copy_move_base(const expected_copy_move_base &) = default;
expected_copy_move_base(expected_copy_move_base &&) noexcept = default;
expected_copy_move_base &operator=(const expected_copy_move_base &) = delete;
expected_copy_move_base &
operator=(expected_copy_move_base &&) noexcept = default;
};
template
struct expected_copy_move_base {
expected_copy_move_base() = default;
~expected_copy_move_base() = default;
expected_copy_move_base(const expected_copy_move_base &) = default;
expected_copy_move_base(expected_copy_move_base &&) noexcept = default;
expected_copy_move_base &operator=(const expected_copy_move_base &) = delete;
expected_copy_move_base &
operator=(expected_copy_move_base &&) noexcept = delete;
};
template
struct expected_copy_move_base {
expected_copy_move_base() = default;
~expected_copy_move_base() = default;
expected_copy_move_base(const expected_copy_move_base &) = default;
expected_copy_move_base &operator=(const expected_copy_move_base &) = default;
expected_copy_move_base &
operator=(expected_copy_move_base &&) noexcept = default;
};
template
struct expected_copy_move_base {
expected_copy_move_base() = default;
~expected_copy_move_base() = default;
expected_copy_move_base(const expected_copy_move_base &) = default;
expected_copy_move_base &operator=(const expected_copy_move_base &) = default;
expected_copy_move_base &
operator=(expected_copy_move_base &&) noexcept = delete;
};
template
struct expected_copy_move_base {
expected_copy_move_base() = default;
~expected_copy_move_base() = default;
expected_copy_move_base(const expected_copy_move_base &) = default;
expected_copy_move_base &operator=(const expected_copy_move_base &) = delete;
expected_copy_move_base &
operator=(expected_copy_move_base &&) noexcept = default;
};
template
struct expected_copy_move_base {
expected_copy_move_base() = default;
~expected_copy_move_base() = default;
expected_copy_move_base(const expected_copy_move_base &) = default;
expected_copy_move_base &operator=(const expected_copy_move_base &) = delete;
expected_copy_move_base &
operator=(expected_copy_move_base &&) noexcept = delete;
};
template
struct expected_copy_move_base {
expected_copy_move_base() = default;
~expected_copy_move_base() = default;
expected_copy_move_base(const expected_copy_move_base &) = delete;
expected_copy_move_base &operator=(const expected_copy_move_base &) = default;
expected_copy_move_base &
operator=(expected_copy_move_base &&) noexcept = default;
};
template
struct expected_copy_move_base {
expected_copy_move_base() = default;
~expected_copy_move_base() = default;
expected_copy_move_base(const expected_copy_move_base &) = delete;
expected_copy_move_base &operator=(const expected_copy_move_base &) = default;
expected_copy_move_base &
operator=(expected_copy_move_base &&) noexcept = delete;
};
template
struct expected_copy_move_base {
expected_copy_move_base() = default;
~expected_copy_move_base() = default;
expected_copy_move_base(const expected_copy_move_base &) = delete;
expected_copy_move_base &operator=(const expected_copy_move_base &) = delete;
expected_copy_move_base &
operator=(expected_copy_move_base &&) noexcept = default;
};
template
struct expected_copy_move_base {
expected_copy_move_base() = default;
~expected_copy_move_base() = default;
expected_copy_move_base(const expected_copy_move_base &) = delete;
expected_copy_move_base &operator=(const expected_copy_move_base &) = delete;
expected_copy_move_base &
operator=(expected_copy_move_base &&) noexcept = delete;
};
template
struct expected_copy_move_base {
expected_copy_move_base() = default;
~expected_copy_move_base() = default;
expected_copy_move_base(const expected_copy_move_base &) = delete;
expected_copy_move_base(expected_copy_move_base &&) noexcept = default;
expected_copy_move_base &operator=(const expected_copy_move_base &) = default;
expected_copy_move_base &
operator=(expected_copy_move_base &&) noexcept = default;
};
template
struct expected_copy_move_base {
expected_copy_move_base() = default;
~expected_copy_move_base() = default;
expected_copy_move_base(const expected_copy_move_base &) = delete;
expected_copy_move_base(expected_copy_move_base &&) noexcept = default;
expected_copy_move_base &operator=(const expected_copy_move_base &) = default;
expected_copy_move_base &
operator=(expected_copy_move_base &&) noexcept = delete;
};
template
struct expected_copy_move_base {
expected_copy_move_base() = default;
~expected_copy_move_base() = default;
expected_copy_move_base(const expected_copy_move_base &) = delete;
expected_copy_move_base(expected_copy_move_base &&) noexcept = default;
expected_copy_move_base &operator=(const expected_copy_move_base &) = delete;
expected_copy_move_base &
operator=(expected_copy_move_base &&) noexcept = default;
};
template
struct expected_copy_move_base {
expected_copy_move_base() = default;
~expected_copy_move_base() = default;
expected_copy_move_base(const expected_copy_move_base &) = delete;
expected_copy_move_base(expected_copy_move_base &&) noexcept = default;
expected_copy_move_base &operator=(const expected_copy_move_base &) = delete;
expected_copy_move_base &
operator=(expected_copy_move_base &&) noexcept = delete;
};
// This is needed to be able to construct the expected_default_ctor_base which
// follows, while still conditionally deleting the default constructor.
struct default_constructor_tag {
explicit constexpr default_constructor_tag() = default;
};
// expected_default_ctor_base will ensure that expected has a deleted default
// consturctor if T is not default constructible.
// This specialization is for when T is default constructible
template ::value>
struct expected_default_ctor_base {
constexpr expected_default_ctor_base() noexcept = default;
constexpr expected_default_ctor_base(
expected_default_ctor_base const &) noexcept = default;
constexpr expected_default_ctor_base(expected_default_ctor_base &&) noexcept =
default;
expected_default_ctor_base &
operator=(expected_default_ctor_base const &) noexcept = default;
expected_default_ctor_base &
operator=(expected_default_ctor_base &&) noexcept = default;
constexpr explicit expected_default_ctor_base(default_constructor_tag) {}
};
// This specialization is for when T is not default constructible
template struct expected_default_ctor_base {
constexpr expected_default_ctor_base() noexcept = delete;
constexpr expected_default_ctor_base(
expected_default_ctor_base const &) noexcept = default;
constexpr expected_default_ctor_base(expected_default_ctor_base &&) noexcept =
default;
expected_default_ctor_base &
operator=(expected_default_ctor_base const &) noexcept = default;
expected_default_ctor_base &
operator=(expected_default_ctor_base &&) noexcept = default;
constexpr explicit expected_default_ctor_base(default_constructor_tag) {}
};
// expected_impl provides indirection for the copy/move constructor/assignment
// operators, so those in expected itself can be declared `=default`. This
// allows the base classes for conditionally deleting those special member
// functions to work.
template
struct expected_impl : protected expected_storage_base {
T *valptr() { return std::addressof(this->m_val); }
unexpected *errptr() { return std::addressof(this->m_unexpect); }
T &val() { return this->m_val; }
unexpected &err() { return this->m_unexpect; }
const T &val() const { return this->m_val; }
const unexpected &err() const { return this->m_unexpect; }
constexpr const T &operator*() const & { return val(); }
constexpr T &operator*() & { return val(); }
constexpr const T &&operator*() const && { return std::move(val()); }
constexpr T &&operator*() && { return std::move(val()); }
using storage_base = detail::expected_storage_base;
using storage_base::storage_base;
constexpr expected_impl() = default;
constexpr expected_impl(const expected_impl &rhs) {
if (this->m_has_val) {
::new (valptr()) T(*rhs);
} else {
::new (errptr()) unexpected(rhs.m_unexpect);
}
}
constexpr expected_impl(expected_impl &&rhs) noexcept(
std::is_nothrow_move_constructible::value
&&std::is_nothrow_move_constructible::value) {
if (rhs.m_has_val) {
::new (valptr()) T(std::move(rhs.m_val));
} else {
::new (errptr()) unexpected(std::move(rhs.m_unexpect));
}
}
expected_impl &operator=(const expected_impl &rhs) { return assign(rhs); }
expected_impl &operator=(expected_impl &&rhs) {
return assign(std::move(rhs));
}
// These assign overloads ensure that the most efficient assignment
// implementation is used while maintaining the strong exception guarantee.
// The problematic case is where rhs has a value, but *this does not.
//
// This overload handles the case where we can just copy-construct `T`
// directly into place without throwing.
template ::value>
* = nullptr>
expected_impl &assign(const expected_impl &rhs) noexcept {
if (!this->m_has_val && rhs.m_has_val) {
err().~unexpected();
::new (valptr()) T(*rhs);
this->m_has_val = true;
return *this;
}
return assign_common(rhs);
}
// This overload handles the case where we can attempt to create a copy of
// `T`, then no-throw move it into place if the copy was successful.
template ::value &&
std::is_nothrow_move_constructible::value>
* = nullptr>
expected_impl &assign(const expected_impl &rhs) noexcept {
if (!this->m_has_val && rhs.m_has_val) {
T tmp = *rhs;
err().~unexpected();
::new (valptr()) T(std::move(tmp));
this->m_has_val = true;
return *this;
}
return assign_common(rhs);
}
// This overload is the worst-case, where we have to move-construct the
// unexpected value into temporary storage, then try to copy the T into place.
// If the construction succeeds, then everything is fine, but if it throws,
// then we move the old unexpected value back into place before rethrowing the
// exception.
template ::value &&
!std::is_nothrow_move_constructible::value>
* = nullptr>
expected_impl &assign(const expected_impl &rhs) {
if (!this->m_has_val && rhs.m_has_val) {
auto tmp = std::move(err());
err().~unexpected();
try {
::new (valptr()) T(*rhs);
this->m_has_val = true;
} catch (...) {
err() = std::move(tmp);
throw;
}
return *this;
}
return assign_common(rhs);
}
// These overloads do the same as above, but for rvalues
template ::value>
* = nullptr>
expected_impl &assign(expected_impl &&rhs) noexcept {
if (!this->m_has_val && rhs.m_has_val) {
err().~unexpected();
::new (valptr()) T(*std::move(rhs));
this->m_has_val = true;
return *this;
}
return assign_common(rhs);
}
template ::value>
* = nullptr>
expected_impl &assign(expected_impl &&rhs) {
if (!this->m_has_val && rhs.m_has_val) {
auto tmp = std::move(err());
err().~unexpected();
try {
::new (valptr()) T(*std::move(rhs));
this->m_has_val = true;
} catch (...) {
err() = std::move(tmp);
throw;
}
return *this;
}
return assign_common(rhs);
}
// The common part of move/copy assigning
template expected_impl &assign_common(Rhs &&rhs) {
if (this->m_has_val) {
if (rhs.m_has_val) {
val() = *std::forward(rhs);
} else {
val().~T();
::new (errptr()) unexpected(std::forward(rhs).err());
}
} else {
if (!rhs.m_has_val) {
err() = std::forward(rhs).err();
}
}
return *this;
}
};
} // namespace detail
template class bad_expected_access : public std::exception {
public:
explicit bad_expected_access(E e) : m_val(std::move(e)) {}
virtual const char *what() const noexcept override {
return "Bad expected access";
}
const E &error() const & { return m_val; }
E &error() & { return m_val; }
const E &&error() const && { return std::move(m_val); }
E &&error() && { return std::move(m_val); }
private:
E m_val;
};
/// An `expected` object is an object that contains the storage for
/// another object and manages the lifetime of this contained object `T`.
/// Alternatively it could contain the storage for another unexpected object
/// `E`. The contained object may not be initialized after the expected object
/// has been initialized, and may not be destroyed before the expected object
/// has been destroyed. The initialization state of the contained object is
/// tracked by the expected object.
template
class expected : private detail::expected_impl,
private detail::expected_copy_move_base,
private detail::expected_default_ctor_base {
static_assert(!std::is_reference::value, "T must not be a reference");
static_assert(!std::is_same>::value,
"T must not be in_place_t");
static_assert(!std::is_same>::value,
"T must not be unexpect_t");
static_assert(!std::is_same>>::value,
"T must not be unexpected");
static_assert(!std::is_reference::value, "E must not be a reference");
static_assert(!std::is_same::value, "T must not be void");
T *valptr() { return std::addressof(this->m_val); }
unexpected *errptr() { return std::addressof(this->m_unexpect); }
T &val() { return this->m_val; }
unexpected &err() { return this->m_unexpect; }
const T &val() const { return this->m_val; }
const unexpected &err() const { return this->m_unexpect; }
using impl_base = detail::expected_impl;
using ctor_base = detail::expected_default_ctor_base;
public:
typedef T value_type;
typedef E error_type;
typedef unexpected unexpected_type;
#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), value())`
/// returns a `std::optional` for some `U`. \returns Let `U` be the result
/// of `std::invoke(std::forward(f), value())`. Returns a
/// `std::optional`. The return value is empty if `*this` is empty,
/// otherwise the return value of `std::invoke(std::forward(f), value())`
/// is returned. \group and_then \synopsis template \nconstexpr auto
/// and_then(F &&f) &;
template TL_EXPECTED_11_CONSTEXPR auto and_then(F &&f) & {
using result = detail::invoke_result_t;
static_assert(detail::is_expected::value,
"F must return an expected");
return has_value() ? detail::invoke(std::forward(f), **this)
: result(unexpect, this->error());
}
/// \group and_then
/// \synopsis template \nconstexpr auto and_then(F &&f) &&;
template TL_EXPECTED_11_CONSTEXPR auto and_then(F &&f) && {
using result = detail::invoke_result_t;
static_assert(detail::is_expected::value,
"F must return an expected");
return has_value() ? detail::invoke(std::forward(f), std::move(**this))
: result(unexpect, std::move(this->error()));
}
/// \group and_then
/// \synopsis template \nconstexpr auto and_then(F &&f) const &;
template constexpr auto and_then(F &&f) const & {
using result = detail::invoke_result_t;
static_assert(detail::is_expected::value,
"F must return an expected");
return has_value() ? detail::invoke(std::forward(f), **this)
: result(unexpect, this->error());
}
#ifndef TL_EXPECTED_NO_CONSTRR
/// \group and_then
/// \synopsis template \nconstexpr auto and_then(F &&f) const &&;
template constexpr auto and_then(F &&f) const && {
using result = detail::invoke_result_t;
static_assert(detail::is_expected::value,
"F must return an expected");
return has_value() ? detail::invoke(std::forward(f), std::move(**this))
: result(unexpect, std::move(this->error()));
}
#endif
#else
/// \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), value())`
/// returns a `std::optional` for some `U`. \returns Let `U` be the result
/// of `std::invoke(std::forward(f), value())`. Returns a
/// `std::optional`. The return value is empty if `*this` is empty,
/// otherwise the return value of `std::invoke(std::forward(f), value())`
/// is returned. \group and_then \synopsis template \nconstexpr auto
/// and_then(F &&f) &;
template
TL_EXPECTED_11_CONSTEXPR detail::invoke_result_t and_then(F &&f) & {
using result = detail::invoke_result_t;
static_assert(detail::is_expected::value,
"F must return an expected");
return has_value() ? detail::invoke(std::forward(f), **this)
: result(unexpect, this->error());
}
/// \group and_then
/// \synopsis template \nconstexpr auto and_then(F &&f) &&;
template
TL_EXPECTED_11_CONSTEXPR detail::invoke_result_t and_then(F &&f) && {
using result = detail::invoke_result_t;
static_assert(detail::is_expected::value,
"F must return an expected");
return has_value() ? detail::invoke(std::forward