mirror of
https://github.com/TartanLlama/optional.git
synced 2025-07-31 02:17:14 +02:00
Fix #1
This commit is contained in:
400
optional.hpp
400
optional.hpp
@ -32,7 +32,12 @@
|
||||
#define TL_OPTIONAL_NO_CONSTRR
|
||||
#endif
|
||||
|
||||
#if __cplusplus == 201103L || defined(TL_OPTIONAL_MSVC2015) || defined(TL_OPTIONAL_GCC49)
|
||||
#if __cplusplus > 201103L
|
||||
#define TL_OPTIONAL_CXX14
|
||||
#endif
|
||||
|
||||
#if __cplusplus == 201103L || defined(TL_OPTIONAL_MSVC2015) || \
|
||||
defined(TL_OPTIONAL_GCC49)
|
||||
/// \exclude
|
||||
#define TL_OPTIONAL_11_CONSTEXPR
|
||||
#else
|
||||
@ -40,7 +45,6 @@
|
||||
#define TL_OPTIONAL_11_CONSTEXPR constexpr
|
||||
#endif
|
||||
|
||||
|
||||
namespace tl {
|
||||
/// \brief Used to represent an optional with no data; essentially a bool
|
||||
class monostate {};
|
||||
@ -118,36 +122,20 @@ using invoke_result = invoke_result_impl<F, void, Us...>;
|
||||
template <class F, class... Us>
|
||||
using invoke_result_t = typename invoke_result<F, Us...>::type;
|
||||
|
||||
|
||||
// Change void to tl::monostate
|
||||
template <class U>
|
||||
using fixup_void = conditional_t<std::is_void<U>::value, monostate, U>;
|
||||
|
||||
template <class F, class... U> struct get_invoke_optional_ret {
|
||||
using type = invoke_result_t<
|
||||
conditional_t<std::is_lvalue_reference<F>::value,
|
||||
typename remove_reference_t<F>::value_type &,
|
||||
typename remove_reference_t<F>::value_type &&>,
|
||||
U...>;
|
||||
};
|
||||
|
||||
template <class F, class... U>
|
||||
using get_invoke_ret = typename conditional_t<is_optional<F>::value,
|
||||
get_invoke_optional_ret<F, U...>,
|
||||
invoke_result<F, U...>>::type;
|
||||
|
||||
template <class F, class U>
|
||||
using get_map_return = optional<fixup_void<get_invoke_ret<F, U>>>;
|
||||
template <class F, class U, class = invoke_result_t<F, U>>
|
||||
using get_map_return = optional<fixup_void<invoke_result_t<F, U>>>;
|
||||
|
||||
// Check if invoking F for some Us returns void
|
||||
template <class F, class = void, class... U> struct returns_void_impl;
|
||||
template <class F, class... U>
|
||||
using returns_void = std::is_void<get_invoke_ret<F, U...>>;
|
||||
|
||||
template <class T>
|
||||
using disable_if_optional = enable_if_t<!is_optional<T>::value>;
|
||||
|
||||
template <class T>
|
||||
using enable_if_optional = enable_if_t<is_optional<T>::value>;
|
||||
struct returns_void_impl<F, void_t<invoke_result_t<F, U...>>, U...>
|
||||
: std::is_void<invoke_result_t<F, U...>> {};
|
||||
template <class F, class... U>
|
||||
using returns_void = returns_void_impl<F, void, U...>;
|
||||
|
||||
template <class T, class... U>
|
||||
using enable_if_ret_void = enable_if_t<returns_void<T &&, U...>::value>;
|
||||
@ -270,10 +258,10 @@ struct is_nothrow_swappable
|
||||
};
|
||||
#endif
|
||||
|
||||
|
||||
template <class T, bool = ::std::is_trivially_destructible<T>::value>
|
||||
struct optional_storage_base {
|
||||
TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept : m_dummy(), m_has_value(false) {}
|
||||
TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept
|
||||
: m_dummy(), m_has_value(false) {}
|
||||
|
||||
template <class... U>
|
||||
TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U &&... u) noexcept
|
||||
@ -300,8 +288,7 @@ template <class T> struct optional_storage_base<T, true> {
|
||||
: m_dummy(), m_has_value(false) {}
|
||||
|
||||
template <class... U>
|
||||
TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t,
|
||||
U &&... u) noexcept
|
||||
TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U &&... u) noexcept
|
||||
: m_value(std::forward<U>(u)...), m_has_value(true) {}
|
||||
|
||||
~optional_storage_base() = default;
|
||||
@ -314,6 +301,7 @@ template <class T> struct optional_storage_base<T, true> {
|
||||
|
||||
bool m_has_value = false;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
/// \brief A tag type to represent an empty optional
|
||||
@ -339,18 +327,25 @@ public:
|
||||
const char *what() const noexcept { return "Optional has no value"; }
|
||||
};
|
||||
|
||||
/// An optional object is an object that contains the storage for another object and manages the lifetime of this contained object, if any. The contained object may be initialized after the optional object has been initialized, and may be destroyed before the optional object has been destroyed. The initialization state of the contained object is tracked by the optional object.
|
||||
/// An optional object is an object that contains the storage for another object
|
||||
/// and manages the lifetime of this contained object, if any. The contained
|
||||
/// object may be initialized after the optional object has been initialized,
|
||||
/// and may be destroyed before the optional object has been destroyed. The
|
||||
/// initialization state of the contained object is tracked by the optional
|
||||
/// object.
|
||||
template <class T> class optional : private detail::optional_storage_base<T> {
|
||||
using base = detail::optional_storage_base<T>;
|
||||
|
||||
public:
|
||||
|
||||
/// \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())` returns a `std::optional<U>` for some `U`.
|
||||
/// \returns Let `U` be the result of `std::invoke(std::forward<F>(f), value())`. Returns a `std::optional<U>`. The return value is empty if `*this` is empty, otherwise the return value of `std::invoke(std::forward<F>(f), value())` is returned.
|
||||
/// \group and_then
|
||||
/// \synopsis template <class F>\nconstexpr auto and_then(F &&f) &;
|
||||
/// Carries out some operation which returns an optional on the stored object
|
||||
/// if there is one. \requires `std::invoke(std::forward<F>(f), value())`
|
||||
/// returns a `std::optional<U>` for some `U`. \returns Let `U` be the result
|
||||
/// of `std::invoke(std::forward<F>(f), value())`. Returns a
|
||||
/// `std::optional<U>`. The return value is empty if `*this` is empty,
|
||||
/// otherwise the return value of `std::invoke(std::forward<F>(f), value())`
|
||||
/// is returned. \group and_then \synopsis template <class F>\nconstexpr auto
|
||||
/// and_then(F &&f) &;
|
||||
template <class F>
|
||||
TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t<F, T> and_then(F &&f) & {
|
||||
using result = detail::invoke_result_t<F, T>;
|
||||
@ -399,185 +394,69 @@ public:
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef TL_OPTIONAL_CXX14
|
||||
/// \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::optional<U>`. The return value is empty if `*this` is empty, otherwise an `optional<U>` is constructed from the return value of `std::invoke(std::forward<F>(f), value())` and is returned.
|
||||
/// \group map
|
||||
/// \synopsis template <class F> auto map(F &&f) &;
|
||||
template <class F, detail::disable_if_optional<F> * = nullptr,
|
||||
detail::disable_if_ret_void<F, T &> * = nullptr>
|
||||
detail::get_map_return<F, T &> map(F &&f) &
|
||||
noexcept(noexcept(detail::invoke(std::forward<F>(f),
|
||||
std::declval<T &>()))) {
|
||||
using result = detail::get_map_return<F, T &>;
|
||||
return has_value() ? detail::invoke(std::forward<F>(f), **this)
|
||||
: result(nullopt);
|
||||
/// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
|
||||
/// value())`. Returns a `std::optional<U>`. The return value is empty if
|
||||
/// `*this` is empty, otherwise an `optional<U>` is constructed from the
|
||||
/// return value of `std::invoke(std::forward<F>(f), value())` and is
|
||||
/// returned. \group map \synopsis template <class F> auto map(F &&f) &;
|
||||
template <class F> auto map(F &&f) & {
|
||||
return map_impl(*this, std::forward<F>(f));
|
||||
}
|
||||
|
||||
/// \exclude
|
||||
template <class F, detail::disable_if_optional<F> * = nullptr,
|
||||
detail::enable_if_ret_void<F, T &> * = nullptr>
|
||||
detail::get_map_return<F, T &> map(F &&f) & {
|
||||
if (!has_value())
|
||||
return nullopt;
|
||||
|
||||
detail::invoke(std::forward<F>(f), **this);
|
||||
return monostate{};
|
||||
template <class F> auto map(F &&f) && {
|
||||
return map_impl(std::move(*this), std::forward<F>(f));
|
||||
}
|
||||
|
||||
/// \exclude
|
||||
template <class F, detail::enable_if_optional<F> * = nullptr,
|
||||
detail::disable_if_ret_void<F, T &> * = nullptr>
|
||||
detail::get_map_return<F, T &> map(F &&f) & {
|
||||
using result = detail::get_map_return<F, T &>;
|
||||
return (f.has_value() && has_value())
|
||||
? detail::invoke(*std::forward<F>(f), **this)
|
||||
: result(nullopt);
|
||||
}
|
||||
|
||||
/// \exclude
|
||||
template <class F, detail::enable_if_optional<F> * = nullptr,
|
||||
detail::enable_if_ret_void<F, T &> * = nullptr>
|
||||
detail::get_map_return<F, T &> map(F &&f) & {
|
||||
if (!f.has_value() || !has_value())
|
||||
return nullopt;
|
||||
|
||||
detail::invoke(*std::forward<F>(f), **this);
|
||||
return monostate{};
|
||||
}
|
||||
|
||||
/// \group map
|
||||
/// \synopsis template <class F> auto map(F &&f) &&;
|
||||
template <class F, detail::disable_if_optional<F> * = nullptr,
|
||||
detail::disable_if_ret_void<F, T &&> * = nullptr>
|
||||
detail::get_map_return<F, T &&> map(F &&f) && {
|
||||
using result = detail::get_map_return<F, T &&>;
|
||||
return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this))
|
||||
: result(nullopt);
|
||||
}
|
||||
|
||||
/// \exclude
|
||||
template <class F, detail::disable_if_optional<F> * = nullptr,
|
||||
detail::enable_if_ret_void<F, T &&> * = nullptr>
|
||||
detail::get_map_return<F, T &&> map(F &&f) && {
|
||||
if (!has_value())
|
||||
return nullopt;
|
||||
|
||||
detail::invoke(std::forward<F>(f), std::move(**this));
|
||||
return monostate{};
|
||||
}
|
||||
|
||||
/// \exclude
|
||||
template <class F, detail::enable_if_optional<F> * = nullptr,
|
||||
detail::disable_if_ret_void<F, T &&> * = nullptr>
|
||||
detail::get_map_return<F, T &&> map(F &&f) && {
|
||||
using result = detail::get_map_return<F, T &&>;
|
||||
return (f.has_value() && has_value())
|
||||
? detail::invoke(*std::forward<F>(f), std::move(**this))
|
||||
: result(nullopt);
|
||||
}
|
||||
|
||||
/// \exclude
|
||||
template <class F, detail::enable_if_optional<F> * = nullptr,
|
||||
detail::enable_if_ret_void<F, T &&> * = nullptr>
|
||||
detail::get_map_return<F, T &&> map(F &&f) && {
|
||||
if (!f.has_value() || !has_value())
|
||||
return nullopt;
|
||||
|
||||
detail::invoke(*std::forward<F>(f), std::move(**this));
|
||||
return monostate{};
|
||||
}
|
||||
|
||||
/// \group map
|
||||
/// \synopsis template <class F> auto map(F &&f) const &;
|
||||
template <class F, detail::disable_if_optional<F> * = nullptr,
|
||||
detail::disable_if_ret_void<F, T const &> * = nullptr>
|
||||
constexpr detail::get_map_return<F, T const &> map(F &&f) const & {
|
||||
using result = detail::get_map_return<F, T const &>;
|
||||
return this->has_value()
|
||||
? result(detail::invoke(std::forward<F>(f), **this))
|
||||
: result(nullopt);
|
||||
}
|
||||
|
||||
/// \exclude
|
||||
template <class F, detail::disable_if_optional<F> * = nullptr,
|
||||
detail::enable_if_ret_void<F, T const &> * = nullptr>
|
||||
detail::get_map_return<F, T const &> map(F &&f) const & {
|
||||
if (!has_value())
|
||||
return nullopt;
|
||||
|
||||
detail::invoke(std::forward<F>(f), **this);
|
||||
return monostate{};
|
||||
}
|
||||
|
||||
/// \exclude
|
||||
template <class F, detail::enable_if_optional<F> * = nullptr,
|
||||
detail::disable_if_ret_void<F, T const &> * = nullptr>
|
||||
constexpr detail::get_map_return<F, T const &> map(F &&f) const & {
|
||||
using result = detail::get_map_return<F, const T &>;
|
||||
return (f.has_value() && has_value())
|
||||
? detail::invoke(*std::forward<F>(f), **this)
|
||||
: result(nullopt);
|
||||
}
|
||||
|
||||
/// \exclude
|
||||
template <class F, detail::enable_if_optional<F> * = nullptr,
|
||||
detail::enable_if_ret_void<F, T const &> * = nullptr>
|
||||
detail::get_map_return<F, T const &> map(F &&f) const & {
|
||||
if (!f.has_value() || !has_value())
|
||||
return nullopt;
|
||||
|
||||
detail::invoke(*std::forward<F>(f), **this);
|
||||
return monostate{};
|
||||
template <class F> auto map(F &&f) const & {
|
||||
return map_impl(*this, std::forward<F>(f));
|
||||
}
|
||||
|
||||
#ifndef TL_OPTIONAL_NO_CONSTRR
|
||||
/// \group map
|
||||
/// \synopsis template <class F> auto map(F &&f) const &&;
|
||||
template <class F, detail::disable_if_optional<F> * = nullptr,
|
||||
detail::disable_if_ret_void<F, T const &&> * = nullptr>
|
||||
constexpr detail::get_map_return<F, T const &&> map(F &&f) const && {
|
||||
using result = detail::get_map_return<F, const T &&>;
|
||||
return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this))
|
||||
: result(nullopt);
|
||||
template <class F> auto map(F &&f) const && {
|
||||
return map_impl(std::move(*this), std::forward<F>(f));
|
||||
}
|
||||
#endif
|
||||
#else
|
||||
/// \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::optional<U>`. The return value is empty if
|
||||
/// `*this` is empty, otherwise an `optional<U>` is constructed from the
|
||||
/// return value of `std::invoke(std::forward<F>(f), value())` and is
|
||||
/// returned. \group map \synopsis template <class F> auto map(F &&f) &;
|
||||
template <class F>
|
||||
decltype(map_impl(std::declval<optional &>(), std::declval<F &&>()))
|
||||
map(F &&f) & {
|
||||
return map_impl(*this, std::forward<F>(f));
|
||||
}
|
||||
|
||||
/// \exclude
|
||||
template <class F, detail::disable_if_optional<F> * = nullptr,
|
||||
detail::enable_if_ret_void<F, T const &&> * = nullptr>
|
||||
detail::get_map_return<F, T const &&> map(F &&f) const && {
|
||||
if (!has_value())
|
||||
return nullopt;
|
||||
|
||||
detail::invoke(std::forward<F>(f), std::move(**this));
|
||||
return monostate{};
|
||||
template <class F>
|
||||
decltype(map_impl(std::declval<optional &&>(), std::declval<F &&>()))
|
||||
map(F &&f) && {
|
||||
return map_impl(std::move(*this), std::forward<F>(f));
|
||||
}
|
||||
|
||||
/// \exclude
|
||||
template <class F, detail::enable_if_optional<F> * = nullptr,
|
||||
detail::disable_if_ret_void<F, T const &&> * = nullptr>
|
||||
constexpr detail::get_map_return<F, T const &&> map(F &&f) const && {
|
||||
using result = detail::get_map_return<F, const T &&>;
|
||||
return (f.has_value() && has_value())
|
||||
? detail::invoke(*std::forward<F>(f), std::move(**this))
|
||||
: result(nullopt);
|
||||
template <class F>
|
||||
decltype(map_impl(std::declval<const optional &>(), std::declval<F &&>()))
|
||||
map(F &&f) const & {
|
||||
return map_impl(*this, std::forward<F>(f));
|
||||
}
|
||||
|
||||
/// \exclude
|
||||
template <class F, detail::enable_if_optional<F> * = nullptr,
|
||||
detail::enable_if_ret_void<F, T const &&> * = nullptr>
|
||||
detail::get_map_return<F, T &> map(F &&f) const && {
|
||||
if (!f.has_value() || !has_value())
|
||||
return nullopt;
|
||||
|
||||
detail::invoke(*std::forward<F>(f), std::move(**this));
|
||||
return monostate{};
|
||||
#ifndef TL_OPTIONAL_NO_CONSTRR
|
||||
template <class F>
|
||||
decltype(map_impl(std::declval<const optional &&>(), std::declval<F &&>()))
|
||||
map(F &&f) const && {
|
||||
return map_impl(std::move(*this), std::forward<F>(f));
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/// \brief Calls `f` if the optional is empty
|
||||
/// \requires `std::invoke_result_t<F>` must be void or convertible to `optional<T>`.
|
||||
/// \effects If `*this` has a value, returns `*this`. Otherwise, if `f` returns `void`, calls `std::forward<F>(f)` and returns `std::nullopt`. Otherwise, returns `std::forward<F>(f)()`.
|
||||
/// \group or_else
|
||||
/// \requires `std::invoke_result_t<F>` must be void or convertible to
|
||||
/// `optional<T>`. \effects If `*this` has a value, returns `*this`.
|
||||
/// Otherwise, if `f` returns `void`, calls `std::forward<F>(f)` and returns
|
||||
/// `std::nullopt`. Otherwise, returns `std::forward<F>(f)()`. \group or_else
|
||||
/// \synopsis template <class F> optional<T> or_else (F &&f) &;
|
||||
template <class F, detail::enable_if_ret_void<F> * = nullptr>
|
||||
optional<T> TL_OPTIONAL_11_CONSTEXPR or_else(F &&f) & {
|
||||
@ -646,10 +525,9 @@ public:
|
||||
}
|
||||
#endif
|
||||
|
||||
/// \brief Maps the stored value with `f` if there is one, otherwise returns `u`
|
||||
/// \details If there is a value stored, then `f` is called with `**this` and the value is returned.
|
||||
/// Otherwise `u` is returned.
|
||||
/// \group map_or
|
||||
/// \brief Maps the stored value with `f` if there is one, otherwise returns
|
||||
/// `u` \details If there is a value stored, then `f` is called with `**this`
|
||||
/// and the value is returned. Otherwise `u` is returned. \group map_or
|
||||
template <class F, class U> U map_or(F &&f, U &&u) & {
|
||||
return has_value() ? detail::invoke(std::forward<F>(f), **this)
|
||||
: std::forward<U>(u);
|
||||
@ -675,11 +553,11 @@ public:
|
||||
}
|
||||
#endif
|
||||
|
||||
/// \brief Maps the stored value with `f` if there is one, otherwise calls `u` and returns the result.
|
||||
/// \details If there is a value stored, then `f` is called with `**this` and the value is returned.
|
||||
/// Otherwise `std::forward<U>(u)()` is returned.
|
||||
/// \group map_or_else
|
||||
/// \synopsis template <class F, class U>\nauto map_or_else(F &&f, U &&u) &;
|
||||
/// \brief Maps the stored value with `f` if there is one, otherwise calls `u`
|
||||
/// and returns the result. \details If there is a value stored, then `f` is
|
||||
/// called with `**this` and the value is returned. Otherwise
|
||||
/// `std::forward<U>(u)()` is returned. \group map_or_else \synopsis template
|
||||
/// <class F, class U>\nauto map_or_else(F &&f, U &&u) &;
|
||||
template <class F, class U>
|
||||
detail::invoke_result_t<U> map_or_else(F &&f, U &&u) & {
|
||||
return has_value() ? detail::invoke(std::forward<F>(f), **this)
|
||||
@ -695,7 +573,8 @@ public:
|
||||
}
|
||||
|
||||
/// \group map_or_else
|
||||
/// \synopsis template <class F, class U>\nauto map_or_else(F &&f, U &&u) const &;
|
||||
/// \synopsis template <class F, class U>\nauto map_or_else(F &&f, U &&u)
|
||||
/// const &;
|
||||
template <class F, class U>
|
||||
detail::invoke_result_t<U> map_or_else(F &&f, U &&u) const & {
|
||||
return has_value() ? detail::invoke(std::forward<F>(f), **this)
|
||||
@ -704,7 +583,8 @@ public:
|
||||
|
||||
#ifndef TL_OPTIONAL_NO_CONSTRR
|
||||
/// \group map_or_else
|
||||
/// \synopsis template <class F, class U>\nauto map_or_else(F &&f, U &&u) const &&;
|
||||
/// \synopsis template <class F, class U>\nauto map_or_else(F &&f, U &&u)
|
||||
/// const &&;
|
||||
template <class F, class U>
|
||||
detail::invoke_result_t<U> map_or_else(F &&f, U &&u) const && {
|
||||
return has_value() ? detail::invoke(std::forward<F>(f), std::move(**this))
|
||||
@ -828,10 +708,10 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Constructs the stored value in-place using the given arguments.
|
||||
/// \group in_place
|
||||
/// \synopsis template <class... Args> constexpr explicit optional(in_place_t, Args&&... args);
|
||||
/// \synopsis template <class... Args> constexpr explicit optional(in_place_t,
|
||||
/// Args&&... args);
|
||||
template <class... Args>
|
||||
constexpr explicit optional(
|
||||
detail::enable_if_t<std::is_constructible<T, Args...>::value, in_place_t>,
|
||||
@ -839,7 +719,8 @@ public:
|
||||
: base(in_place, std::forward<Args>(args)...) {}
|
||||
|
||||
/// \group in_place
|
||||
/// \synopsis template <class U, class... Args>\nconstexpr explicit optional(in_place_t, std::initializer_list<U>&, Args&&... args);
|
||||
/// \synopsis template <class U, class... Args>\nconstexpr explicit
|
||||
/// optional(in_place_t, std::initializer_list<U>&, Args&&... args);
|
||||
template <class U, class... Args>
|
||||
TL_OPTIONAL_11_CONSTEXPR explicit optional(
|
||||
detail::enable_if_t<std::is_constructible<T, std::initializer_list<U> &,
|
||||
@ -921,7 +802,8 @@ public:
|
||||
// TODO conditionally delete, check exception guarantee
|
||||
/// Copy assignment.
|
||||
///
|
||||
/// Copies the value from `rhs` if there is one. Otherwise resets the stored value in `*this`.
|
||||
/// Copies the value from `rhs` if there is one. Otherwise resets the stored
|
||||
/// value in `*this`.
|
||||
optional &operator=(const optional &rhs) {
|
||||
if (has_value()) {
|
||||
if (rhs.has_value()) {
|
||||
@ -943,7 +825,8 @@ public:
|
||||
// TODO conditionally delete, check exception guarantee
|
||||
/// Move assignment.
|
||||
///
|
||||
/// Moves the value from `rhs` if there is one. Otherwise resets the stored value in `*this`.
|
||||
/// Moves the value from `rhs` if there is one. Otherwise resets the stored
|
||||
/// value in `*this`.
|
||||
optional &operator=(optional &&rhs) noexcept(
|
||||
std::is_nothrow_move_assignable<T>::value
|
||||
&&std::is_nothrow_move_constructible<T>::value) {
|
||||
@ -965,8 +848,8 @@ public:
|
||||
}
|
||||
|
||||
// TODO conditionally delete, check exception guarantee
|
||||
/// Assigns the stored value from `u`, destroying the old value if there was one.
|
||||
/// \synopsis optional &operator=(U &&u);
|
||||
/// Assigns the stored value from `u`, destroying the old value if there was
|
||||
/// one. \synopsis optional &operator=(U &&u);
|
||||
template <class U = T, detail::enable_assign_forward<T, U> * = nullptr>
|
||||
optional &operator=(U &&u) {
|
||||
if (has_value()) {
|
||||
@ -982,8 +865,8 @@ public:
|
||||
// TODO check exception guarantee
|
||||
/// Converting copy assignment operator.
|
||||
///
|
||||
/// Copies the value from `rhs` if there is one. Otherwise resets the stored value in `*this`.
|
||||
/// \synopsis optional &operator=(const optional<U> & rhs);
|
||||
/// Copies the value from `rhs` if there is one. Otherwise resets the stored
|
||||
/// value in `*this`. \synopsis optional &operator=(const optional<U> & rhs);
|
||||
template <class U,
|
||||
detail::enable_assign_from_other<T, U, const U &> * = nullptr>
|
||||
optional &operator=(const optional<U> &rhs) {
|
||||
@ -1007,8 +890,8 @@ public:
|
||||
// TODO check exception guarantee
|
||||
/// Converting move assignment operator.
|
||||
///
|
||||
/// Moves the value from `rhs` if there is one. Otherwise resets the stored value in `*this`.
|
||||
/// \synopsis optional &operator=(optional<U> && rhs);
|
||||
/// Moves the value from `rhs` if there is one. Otherwise resets the stored
|
||||
/// value in `*this`. \synopsis optional &operator=(optional<U> && rhs);
|
||||
template <class U, detail::enable_assign_from_other<T, U, U> * = nullptr>
|
||||
optional &operator=(optional<U> &&rhs) {
|
||||
if (has_value()) {
|
||||
@ -1039,7 +922,8 @@ public:
|
||||
}
|
||||
|
||||
/// \group emplace
|
||||
/// \synopsis template <class U, class... Args>\nT& emplace(std::initializer_list<U> il, Args &&... args);
|
||||
/// \synopsis template <class U, class... Args>\nT&
|
||||
/// emplace(std::initializer_list<U> il, Args &&... args);
|
||||
template <class U, class... Args>
|
||||
detail::enable_if_t<
|
||||
std::is_constructible<T, std::initializer_list<U> &, Args &&...>::value,
|
||||
@ -1053,7 +937,8 @@ public:
|
||||
///
|
||||
/// If neither optionals have a value, nothing happens.
|
||||
/// If both have a value, the values are swapped.
|
||||
/// If one has a value, it is moved to the other and the movee is left valueless.
|
||||
/// If one has a value, it is moved to the other and the movee is left
|
||||
/// valueless.
|
||||
void
|
||||
swap(optional &rhs) noexcept(std::is_nothrow_move_constructible<T>::value
|
||||
&&detail::is_nothrow_swappable<T>::value) {
|
||||
@ -1114,9 +999,8 @@ public:
|
||||
return this->m_has_value;
|
||||
}
|
||||
|
||||
/// \returns the contained value if there is one, otherwise throws [bad_optional_access]
|
||||
/// \group value
|
||||
/// \synopsis constexpr T &value();
|
||||
/// \returns the contained value if there is one, otherwise throws
|
||||
/// [bad_optional_access] \group value \synopsis constexpr T &value();
|
||||
TL_OPTIONAL_11_CONSTEXPR T &value() & {
|
||||
if (has_value())
|
||||
return this->m_value;
|
||||
@ -1169,15 +1053,14 @@ public:
|
||||
this->m_has_value = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
/// \group relop
|
||||
/// \brief Compares two optional objects
|
||||
/// \details If both optionals contain a value, they are compared with `T`s relational operators.
|
||||
/// Otherwise `lhs` and `rhs` are equal only if they are both empty, and `lhs` is less than `rhs`
|
||||
/// only if `rhs` is empty and `lhs` is not.
|
||||
/// \details If both optionals contain a value, they are compared with `T`s
|
||||
/// relational operators. Otherwise `lhs` and `rhs` are equal only if they are
|
||||
/// both empty, and `lhs` is less than `rhs` only if `rhs` is empty and `lhs` is
|
||||
/// not.
|
||||
template <class T, class U>
|
||||
inline constexpr bool operator==(const optional<T> &lhs,
|
||||
const optional<U> &rhs) {
|
||||
@ -1279,11 +1162,11 @@ inline constexpr bool operator>=(nullopt_t, const optional<T> &rhs) noexcept {
|
||||
return !rhs.has_value();
|
||||
}
|
||||
|
||||
|
||||
/// \group relop_t
|
||||
/// \brief Compares the optional with a value.
|
||||
/// \details If the optional has a value, it is compared with the other value using `T`s relational operators.
|
||||
/// Otherwise, the optional is considered less than the value.
|
||||
/// \details If the optional has a value, it is compared with the other value
|
||||
/// using `T`s relational operators. Otherwise, the optional is considered less
|
||||
/// than the value.
|
||||
template <class T, class U>
|
||||
inline constexpr bool operator==(const optional<T> &lhs, const U &rhs) {
|
||||
return lhs.has_value() ? *lhs == rhs : false;
|
||||
@ -1371,6 +1254,59 @@ inline constexpr optional<T> make_optional(std::initializer_list<U> il,
|
||||
template <class T> optional(T)->optional<T>;
|
||||
#endif
|
||||
|
||||
/// \exclude
|
||||
namespace detail {
|
||||
#ifdef TL_OPTIONAL_CX14
|
||||
template <class Opt, class F,
|
||||
class Ret = decltype(detail::invoke(std::declval<F>(),
|
||||
*std::declval<Opt>())),
|
||||
detail::enable_if_t<!std::is_void<Ret>::value> * = nullptr>
|
||||
auto map_impl(Opt &&opt, F &&f) {
|
||||
return opt.has_value()
|
||||
? detail::invoke(std::forward<F>(f), *std::forward<Opt>(opt))
|
||||
: optional<Ret>(nullopt);
|
||||
}
|
||||
|
||||
template <class Opt, class F,
|
||||
class Ret = decltype(detail::invoke(std::declval<F>(),
|
||||
*std::declval<Opt>())),
|
||||
detail::enable_if_t<std::is_void<Ret>::value> * = nullptr>
|
||||
auto map_impl(Opt &&opt, F &&f) {
|
||||
if (opt.has_value()) {
|
||||
detail::invoke(std::forward<F>(f), *std::forward<Opt>(opt));
|
||||
return monostate{};
|
||||
}
|
||||
|
||||
return optional<Ret>(nullopt);
|
||||
}
|
||||
#else
|
||||
template <class Opt, class F,
|
||||
class Ret = decltype(detail::invoke(std::declval<F>(),
|
||||
*std::declval<Opt>())),
|
||||
detail::enable_if_t<!std::is_void<Ret>::value> * = nullptr>
|
||||
|
||||
auto map_impl(Opt &&opt, F &&f) -> optional<Ret> {
|
||||
return opt.has_value()
|
||||
? detail::invoke(std::forward<F>(f), *std::forward<Opt>(opt))
|
||||
: optional<Ret>(nullopt);
|
||||
}
|
||||
|
||||
template <class Opt, class F,
|
||||
class Ret = decltype(detail::invoke(std::declval<F>(),
|
||||
*std::declval<Opt>())),
|
||||
detail::enable_if_t<std::is_void<Ret>::value> * = nullptr>
|
||||
|
||||
auto map_impl(Opt &&opt, F &&f) -> optional<monostate> {
|
||||
if (opt.has_value()) {
|
||||
detail::invoke(std::forward<F>(f), *std::forward<Opt>(opt));
|
||||
return monostate{};
|
||||
}
|
||||
|
||||
return nullopt;
|
||||
}
|
||||
#endif
|
||||
} // namespace detail
|
||||
|
||||
} // namespace tl
|
||||
|
||||
namespace std {
|
||||
|
@ -12,8 +12,8 @@ constexpr int get_int(int) { return 42; }
|
||||
TL_OPTIONAL_11_CONSTEXPR tl::optional<int> get_opt_int(int) { return 42; }
|
||||
|
||||
// What is Clang Format up to?!
|
||||
TEST_CASE("Monadic operations",
|
||||
"[monadic]"){SECTION("map"){// lhs is empty
|
||||
TEST_CASE("Monadic operations", "[monadic]") {
|
||||
SECTION("map") { // lhs is empty
|
||||
tl::optional<int> o1;
|
||||
auto o1r = o1.map([](int i) { return i + 2; });
|
||||
STATIC_REQUIRE((std::is_same<decltype(o1r), tl::optional<int>>::value));
|
||||
@ -47,20 +47,13 @@ auto o5r = o5.map([](const int &i) { return i + 2; });
|
||||
STATIC_REQUIRE((std::is_same<decltype(o5r), tl::optional<int>>::value));
|
||||
REQUIRE(o5r.value() == 42);
|
||||
|
||||
// test applicative functor
|
||||
tl::optional<int> o6 = 40;
|
||||
auto f6 = tl::make_optional([](const int &i) { return i + 2; });
|
||||
auto o6r = o6.map(f6);
|
||||
STATIC_REQUIRE((std::is_same<decltype(o6r), tl::optional<int>>::value));
|
||||
REQUIRE(o6r.value() == 42);
|
||||
|
||||
// test void return
|
||||
tl::optional<int> o7 = 40;
|
||||
auto f7 = tl::make_optional([](const int &i) { return; });
|
||||
auto f7 = [](const int &i) { return; };
|
||||
auto o7r = o7.map(f7);
|
||||
STATIC_REQUIRE(
|
||||
(std::is_same<decltype(o7r), tl::optional<tl::monostate>>::value));
|
||||
REQUIRE(o6r.has_value());
|
||||
REQUIRE(o7r.has_value());
|
||||
|
||||
// test each overload in turn
|
||||
tl::optional<int> o8 = 42;
|
||||
@ -71,14 +64,6 @@ tl::optional<int> o9 = 42;
|
||||
auto o9r = o9.map([](int) { return; });
|
||||
REQUIRE(o9r);
|
||||
|
||||
tl::optional<int> o10 = 42;
|
||||
auto o10r = o10.map(tl::make_optional([](int) { return 42; }));
|
||||
REQUIRE(*o10r == 42);
|
||||
|
||||
tl::optional<int> o11 = 42;
|
||||
auto o11r = o11.map(tl::make_optional([](int) { return; }));
|
||||
REQUIRE(o11r);
|
||||
|
||||
tl::optional<int> o12 = 42;
|
||||
auto o12r = std::move(o12).map([](int) { return 42; });
|
||||
REQUIRE(*o12r == 42);
|
||||
@ -87,14 +72,6 @@ tl::optional<int> o13 = 42;
|
||||
auto o13r = std::move(o13).map([](int) { return; });
|
||||
REQUIRE(o13r);
|
||||
|
||||
tl::optional<int> o14 = 42;
|
||||
auto o14r = std::move(o14).map(tl::make_optional([](int) { return 42; }));
|
||||
REQUIRE(*o14r == 42);
|
||||
|
||||
tl::optional<int> o15 = 42;
|
||||
auto o15r = std::move(o15).map(tl::make_optional([](int) { return; }));
|
||||
REQUIRE(o15r);
|
||||
|
||||
const tl::optional<int> o16 = 42;
|
||||
auto o16r = o16.map([](int) { return 42; });
|
||||
REQUIRE(*o16r == 42);
|
||||
@ -103,14 +80,6 @@ const tl::optional<int> o17 = 42;
|
||||
auto o17r = o17.map([](int) { return; });
|
||||
REQUIRE(o17r);
|
||||
|
||||
const tl::optional<int> o18 = 42;
|
||||
auto o18r = o18.map(tl::make_optional([](int) { return 42; }));
|
||||
REQUIRE(*o18r == 42);
|
||||
|
||||
const tl::optional<int> o19 = 42;
|
||||
auto o19r = o19.map(tl::make_optional([](int) { return; }));
|
||||
REQUIRE(o19r);
|
||||
|
||||
const tl::optional<int> o20 = 42;
|
||||
auto o20r = std::move(o20).map([](int) { return 42; });
|
||||
REQUIRE(*o20r == 42);
|
||||
@ -119,14 +88,6 @@ const tl::optional<int> o21 = 42;
|
||||
auto o21r = std::move(o21).map([](int) { return; });
|
||||
REQUIRE(o21r);
|
||||
|
||||
const tl::optional<int> o22 = 42;
|
||||
auto o22r = std::move(o22).map(tl::make_optional([](int) { return 42; }));
|
||||
REQUIRE(*o22r == 42);
|
||||
|
||||
const tl::optional<int> o23 = 42;
|
||||
auto o23r = std::move(o23).map(tl::make_optional([](int) { return; }));
|
||||
REQUIRE(o23r);
|
||||
|
||||
tl::optional<int> o24 = tl::nullopt;
|
||||
auto o24r = o24.map([](int) { return 42; });
|
||||
REQUIRE(!o24r);
|
||||
@ -135,14 +96,6 @@ tl::optional<int> o25 = tl::nullopt;
|
||||
auto o25r = o25.map([](int) { return; });
|
||||
REQUIRE(!o25r);
|
||||
|
||||
tl::optional<int> o26 = tl::nullopt;
|
||||
auto o26r = o26.map(tl::make_optional([](int) { return 42; }));
|
||||
REQUIRE(!o26r);
|
||||
|
||||
tl::optional<int> o27 = tl::nullopt;
|
||||
auto o27r = o27.map(tl::make_optional([](int) { return; }));
|
||||
REQUIRE(!o27r);
|
||||
|
||||
tl::optional<int> o28 = tl::nullopt;
|
||||
auto o28r = std::move(o28).map([](int) { return 42; });
|
||||
REQUIRE(!o28r);
|
||||
@ -151,14 +104,6 @@ tl::optional<int> o29 = tl::nullopt;
|
||||
auto o29r = std::move(o29).map([](int) { return; });
|
||||
REQUIRE(!o29r);
|
||||
|
||||
tl::optional<int> o30 = tl::nullopt;
|
||||
auto o30r = std::move(o30).map(tl::make_optional([](int) { return 42; }));
|
||||
REQUIRE(!o30r);
|
||||
|
||||
tl::optional<int> o31 = tl::nullopt;
|
||||
auto o31r = std::move(o31).map(tl::make_optional([](int) { return; }));
|
||||
REQUIRE(!o31r);
|
||||
|
||||
const tl::optional<int> o32 = tl::nullopt;
|
||||
auto o32r = o32.map([](int) { return 42; });
|
||||
REQUIRE(!o32r);
|
||||
@ -167,14 +112,6 @@ const tl::optional<int> o33 = tl::nullopt;
|
||||
auto o33r = o33.map([](int) { return; });
|
||||
REQUIRE(!o33r);
|
||||
|
||||
const tl::optional<int> o34 = tl::nullopt;
|
||||
auto o34r = o34.map(tl::make_optional([](int) { return 42; }));
|
||||
REQUIRE(!o34r);
|
||||
|
||||
const tl::optional<int> o35 = tl::nullopt;
|
||||
auto o35r = o35.map(tl::make_optional([](int) { return; }));
|
||||
REQUIRE(!o35r);
|
||||
|
||||
const tl::optional<int> o36 = tl::nullopt;
|
||||
auto o36r = std::move(o36).map([](int) { return 42; });
|
||||
REQUIRE(!o36r);
|
||||
@ -182,14 +119,6 @@ REQUIRE(!o36r);
|
||||
const tl::optional<int> o37 = tl::nullopt;
|
||||
auto o37r = std::move(o37).map([](int) { return; });
|
||||
REQUIRE(!o37r);
|
||||
|
||||
const tl::optional<int> o38 = tl::nullopt;
|
||||
auto o38r = std::move(o38).map(tl::make_optional([](int) { return 42; }));
|
||||
REQUIRE(!o38r);
|
||||
|
||||
const tl::optional<int> o39 = tl::nullopt;
|
||||
auto o39r = std::move(o39).map(tl::make_optional([](int) { return; }));
|
||||
REQUIRE(!o39r);
|
||||
}
|
||||
|
||||
SECTION("map constexpr") {
|
||||
@ -277,7 +206,8 @@ SECTION("and_then") {
|
||||
|
||||
// ensure that function object is const-propagated
|
||||
const tl::optional<int> o7 = 42;
|
||||
auto o7r = o7.and_then([](const int &i) { return tl::optional<double>(i); });
|
||||
auto o7r =
|
||||
o7.and_then([](const int &i) { return tl::optional<double>(i); });
|
||||
STATIC_REQUIRE((std::is_same<decltype(o7r), tl::optional<double>>::value));
|
||||
REQUIRE(o7r.value() == 42);
|
||||
|
||||
@ -382,10 +312,12 @@ SECTION("map_or") {
|
||||
|
||||
SECTION("map_or_else") {
|
||||
tl::optional<int> o1 = 21;
|
||||
REQUIRE((o1.map_or_else([](int x) { return x * 2; }, []{return 13;})) == 42);
|
||||
REQUIRE((o1.map_or_else([](int x) { return x * 2; }, [] { return 13; })) ==
|
||||
42);
|
||||
|
||||
tl::optional<int> o2;
|
||||
REQUIRE((o2.map_or_else([](int x) { return x * 2; }, []{return 13;})) == 13);
|
||||
REQUIRE((o2.map_or_else([](int x) { return x * 2; }, [] { return 13; })) ==
|
||||
13);
|
||||
}
|
||||
|
||||
SECTION("take") {
|
||||
@ -397,5 +329,17 @@ SECTION("take") {
|
||||
REQUIRE(!o2.take());
|
||||
REQUIRE(!o2);
|
||||
}
|
||||
|
||||
struct foo {
|
||||
void non_const() {}
|
||||
};
|
||||
|
||||
#ifdef TL_OPTIONAL_CXX14
|
||||
// Reported by Mark Papadakis
|
||||
SECTION("map const correctness") {
|
||||
tl::optional<foo> f = foo{};
|
||||
auto l = [](auto &&x) { x.non_const(); };
|
||||
f.map(l);
|
||||
}
|
||||
;
|
||||
#endif
|
||||
};
|
||||
|
Reference in New Issue
Block a user