mirror of
https://github.com/TartanLlama/expected.git
synced 2025-08-02 02:14:31 +02:00
@@ -66,30 +66,30 @@
|
||||
#define TL_EXPECTED_IS_TRIVIALLY_DESTRUCTIBLE(T) \
|
||||
std::is_trivially_destructible<T>
|
||||
|
||||
// GCC 5 < v < 8 has a bug in is_trivially_copy_constructible which breaks std::vector
|
||||
// for non-copyable types
|
||||
#elif (defined(__GNUC__) && __GNUC__ < 8 && \
|
||||
!defined(__clang__))
|
||||
// GCC 5 < v < 8 has a bug in is_trivially_copy_constructible which breaks
|
||||
// std::vector for non-copyable types
|
||||
#elif (defined(__GNUC__) && __GNUC__ < 8 && !defined(__clang__))
|
||||
#ifndef TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX
|
||||
#define TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX
|
||||
namespace tl {
|
||||
namespace detail {
|
||||
template <class T>
|
||||
struct is_trivially_copy_constructible : std::is_trivially_copy_constructible<T>{};
|
||||
struct is_trivially_copy_constructible
|
||||
: std::is_trivially_copy_constructible<T> {};
|
||||
#ifdef _GLIBCXX_VECTOR
|
||||
template <class T, class A>
|
||||
struct is_trivially_copy_constructible<std::vector<T,A>>
|
||||
: std::false_type{};
|
||||
struct is_trivially_copy_constructible<std::vector<T, A>> : std::false_type {};
|
||||
#endif
|
||||
}
|
||||
}
|
||||
} // namespace detail
|
||||
} // namespace tl
|
||||
#endif
|
||||
|
||||
#define TL_EXPECTED_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \
|
||||
tl::detail::is_trivially_copy_constructible<T>
|
||||
#define TL_EXPECTED_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \
|
||||
std::is_trivially_copy_assignable<T>
|
||||
#define TL_EXPECTED_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible<T>
|
||||
#define TL_EXPECTED_IS_TRIVIALLY_DESTRUCTIBLE(T) \
|
||||
std::is_trivially_destructible<T>
|
||||
#else
|
||||
#define TL_EXPECTED_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \
|
||||
std::is_trivially_copy_constructible<T>
|
||||
@@ -138,7 +138,6 @@ public:
|
||||
|
||||
constexpr explicit unexpected(E &&e) : m_val(std::move(e)) {}
|
||||
|
||||
|
||||
template <class... Args, typename std::enable_if<std::is_constructible<
|
||||
E, Args &&...>::value>::type * = nullptr>
|
||||
constexpr explicit unexpected(Args &&...args)
|
||||
@@ -147,8 +146,7 @@ public:
|
||||
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(std::initializer_list<U> l, Args &&...args)
|
||||
: m_val(l, std::forward<Args>(args)...) {}
|
||||
|
||||
constexpr const E &value() const & { return m_val; }
|
||||
@@ -161,8 +159,7 @@ private:
|
||||
};
|
||||
|
||||
#ifdef __cpp_deduction_guides
|
||||
template<class E>
|
||||
unexpected(E) -> unexpected<E>;
|
||||
template <class E> unexpected(E) -> unexpected<E>;
|
||||
#endif
|
||||
|
||||
template <class E>
|
||||
@@ -241,19 +238,26 @@ struct conjunction<B, Bs...>
|
||||
// which results in a hard-error when using it in a noexcept expression
|
||||
// in some cases. This is a check to workaround the common failing case.
|
||||
#ifdef TL_TRAITS_LIBCXX_MEM_FN_WORKAROUND
|
||||
template <class T> struct is_pointer_to_non_const_member_func : std::false_type {};
|
||||
template <class T>
|
||||
struct is_pointer_to_non_const_member_func : std::false_type {};
|
||||
template <class T, class Ret, class... Args>
|
||||
struct is_pointer_to_non_const_member_func<Ret(T::*) (Args...)> : std::true_type {};
|
||||
struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...)>
|
||||
: std::true_type {};
|
||||
template <class T, class Ret, class... Args>
|
||||
struct is_pointer_to_non_const_member_func<Ret(T::*) (Args...)&> : std::true_type {};
|
||||
struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) &>
|
||||
: std::true_type {};
|
||||
template <class T, class Ret, class... Args>
|
||||
struct is_pointer_to_non_const_member_func<Ret(T::*) (Args...) &&> : std::true_type {};
|
||||
struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) &&>
|
||||
: std::true_type {};
|
||||
template <class T, class Ret, class... Args>
|
||||
struct is_pointer_to_non_const_member_func<Ret(T::*) (Args...) volatile> : std::true_type {};
|
||||
struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) volatile>
|
||||
: std::true_type {};
|
||||
template <class T, class Ret, class... Args>
|
||||
struct is_pointer_to_non_const_member_func<Ret(T::*) (Args...) volatile &> : std::true_type {};
|
||||
struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) volatile &>
|
||||
: std::true_type {};
|
||||
template <class T, class Ret, class... Args>
|
||||
struct is_pointer_to_non_const_member_func<Ret(T::*) (Args...) volatile &&> : std::true_type {};
|
||||
struct is_pointer_to_non_const_member_func<Ret (T::*)(Args...) volatile &&>
|
||||
: std::true_type {};
|
||||
|
||||
template <class T> struct is_const_or_const_ref : std::false_type {};
|
||||
template <class T> struct is_const_or_const_ref<T const &> : std::true_type {};
|
||||
@@ -262,13 +266,13 @@ template <class T> struct is_const_or_const_ref<T const> : std::true_type {};
|
||||
|
||||
// std::invoke from C++17
|
||||
// https://stackoverflow.com/questions/38288042/c11-14-invoke-workaround
|
||||
template <typename Fn, typename... Args,
|
||||
template <
|
||||
typename Fn, typename... Args,
|
||||
#ifdef TL_TRAITS_LIBCXX_MEM_FN_WORKAROUND
|
||||
typename = enable_if_t<!(is_pointer_to_non_const_member_func<Fn>::value
|
||||
&& is_const_or_const_ref<Args...>::value)>,
|
||||
typename = enable_if_t<!(is_pointer_to_non_const_member_func<Fn>::value &&
|
||||
is_const_or_const_ref<Args...>::value)>,
|
||||
#endif
|
||||
typename = enable_if_t<std::is_member_pointer<decay_t<Fn>>::value>,
|
||||
int = 0>
|
||||
typename = enable_if_t<std::is_member_pointer<decay_t<Fn>>::value>, int = 0>
|
||||
constexpr auto invoke(Fn &&f, Args &&...args) noexcept(
|
||||
noexcept(std::mem_fn(f)(std::forward<Args>(args)...)))
|
||||
-> decltype(std::mem_fn(f)(std::forward<Args>(args)...)) {
|
||||
@@ -288,9 +292,11 @@ template <class F, class, class... Us> struct invoke_result_impl;
|
||||
|
||||
template <class F, class... Us>
|
||||
struct invoke_result_impl<
|
||||
F, decltype(detail::invoke(std::declval<F>(), std::declval<Us>()...), void()),
|
||||
F,
|
||||
decltype(detail::invoke(std::declval<F>(), std::declval<Us>()...), void()),
|
||||
Us...> {
|
||||
using type = decltype(detail::invoke(std::declval<F>(), std::declval<Us>()...));
|
||||
using type =
|
||||
decltype(detail::invoke(std::declval<F>(), std::declval<Us>()...));
|
||||
};
|
||||
|
||||
template <class F, class... Us>
|
||||
@@ -355,8 +361,8 @@ struct is_swappable<T[N], T[N]>
|
||||
: std::integral_constant<
|
||||
bool,
|
||||
decltype(detail::swap_adl_tests::can_swap<T[N], T[N]>(0))::value &&
|
||||
(!decltype(
|
||||
detail::swap_adl_tests::uses_std<T[N], T[N]>(0))::value ||
|
||||
(!decltype(detail::swap_adl_tests::uses_std<T[N], T[N]>(
|
||||
0))::value ||
|
||||
is_swappable<T, T>::value)> {};
|
||||
|
||||
template <class T, class U = T>
|
||||
@@ -364,12 +370,10 @@ struct is_nothrow_swappable
|
||||
: std::integral_constant<
|
||||
bool,
|
||||
is_swappable<T, U>::value &&
|
||||
((decltype(detail::swap_adl_tests::uses_std<T, U>(0))::value
|
||||
&& detail::swap_adl_tests::is_std_swap_noexcept<T>::value) ||
|
||||
((decltype(detail::swap_adl_tests::uses_std<T, U>(0))::value &&
|
||||
detail::swap_adl_tests::is_std_swap_noexcept<T>::value) ||
|
||||
(!decltype(detail::swap_adl_tests::uses_std<T, U>(0))::value &&
|
||||
detail::swap_adl_tests::is_adl_swap_noexcept<T,
|
||||
U>::value))> {
|
||||
};
|
||||
detail::swap_adl_tests::is_adl_swap_noexcept<T, U>::value))> {};
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -411,14 +415,10 @@ using is_move_constructible_or_void =
|
||||
is_void_or<T, std::is_move_constructible<T>>;
|
||||
|
||||
template <class T>
|
||||
using is_copy_assignable_or_void =
|
||||
is_void_or<T, std::is_copy_assignable<T>>;
|
||||
|
||||
using is_copy_assignable_or_void = is_void_or<T, std::is_copy_assignable<T>>;
|
||||
|
||||
template <class T>
|
||||
using is_move_assignable_or_void =
|
||||
is_void_or<T, std::is_move_assignable<T>>;
|
||||
|
||||
using is_move_assignable_or_void = is_void_or<T, std::is_move_assignable<T>>;
|
||||
|
||||
} // namespace detail
|
||||
|
||||
@@ -857,9 +857,7 @@ struct expected_operations_base : expected_storage_base<T, E> {
|
||||
}
|
||||
#endif
|
||||
|
||||
TL_EXPECTED_11_CONSTEXPR void destroy_val() {
|
||||
get().~T();
|
||||
}
|
||||
TL_EXPECTED_11_CONSTEXPR void destroy_val() { get().~T(); }
|
||||
};
|
||||
|
||||
// This base class provides some handy member functions which can be used in
|
||||
@@ -1238,14 +1236,17 @@ class expected : private detail::expected_move_assign_base<T, E>,
|
||||
"T must not be in_place_t");
|
||||
static_assert(!std::is_same<T, std::remove_cv<unexpect_t>::type>::value,
|
||||
"T must not be unexpect_t");
|
||||
static_assert(!std::is_same<T, typename std::remove_cv<unexpected<E>>::type>::value,
|
||||
static_assert(
|
||||
!std::is_same<T, typename std::remove_cv<unexpected<E>>::type>::value,
|
||||
"T must not be unexpected<E>");
|
||||
static_assert(!std::is_reference<E>::value, "E must not be a reference");
|
||||
|
||||
T *valptr() { return std::addressof(this->m_val); }
|
||||
const T *valptr() const { return std::addressof(this->m_val); }
|
||||
unexpected<E> *errptr() { return std::addressof(this->m_unexpect); }
|
||||
const unexpected<E> *errptr() const { return std::addressof(this->m_unexpect); }
|
||||
const unexpected<E> *errptr() const {
|
||||
return std::addressof(this->m_unexpect);
|
||||
}
|
||||
|
||||
template <class U = T,
|
||||
detail::enable_if_t<!std::is_void<U>::value> * = nullptr>
|
||||
@@ -1290,24 +1291,26 @@ public:
|
||||
#else
|
||||
template <class F>
|
||||
TL_EXPECTED_11_CONSTEXPR auto
|
||||
and_then(F &&f) & -> decltype(and_then_impl(std::declval<expected&>(), std::forward<F>(f))) {
|
||||
and_then(F &&f) & -> decltype(and_then_impl(std::declval<expected &>(),
|
||||
std::forward<F>(f))) {
|
||||
return and_then_impl(*this, std::forward<F>(f));
|
||||
}
|
||||
template <class F>
|
||||
TL_EXPECTED_11_CONSTEXPR auto and_then(F &&f) && -> decltype(
|
||||
and_then_impl(std::declval<expected&&>(), std::forward<F>(f))) {
|
||||
TL_EXPECTED_11_CONSTEXPR auto
|
||||
and_then(F &&f) && -> decltype(and_then_impl(std::declval<expected &&>(),
|
||||
std::forward<F>(f))) {
|
||||
return and_then_impl(std::move(*this), std::forward<F>(f));
|
||||
}
|
||||
template <class F>
|
||||
constexpr auto and_then(F &&f) const & -> decltype(
|
||||
and_then_impl(std::declval<expected const&>(), std::forward<F>(f))) {
|
||||
constexpr auto and_then(F &&f) const & -> decltype(and_then_impl(
|
||||
std::declval<expected const &>(), std::forward<F>(f))) {
|
||||
return and_then_impl(*this, std::forward<F>(f));
|
||||
}
|
||||
|
||||
#ifndef TL_EXPECTED_NO_CONSTRR
|
||||
template <class F>
|
||||
constexpr auto and_then(F &&f) const && -> decltype(
|
||||
and_then_impl(std::declval<expected const&&>(), std::forward<F>(f))) {
|
||||
constexpr auto and_then(F &&f) const && -> decltype(and_then_impl(
|
||||
std::declval<expected const &&>(), std::forward<F>(f))) {
|
||||
return and_then_impl(std::move(*this), std::forward<F>(f));
|
||||
}
|
||||
#endif
|
||||
@@ -1329,14 +1332,14 @@ public:
|
||||
}
|
||||
#else
|
||||
template <class F>
|
||||
TL_EXPECTED_11_CONSTEXPR decltype(
|
||||
expected_map_impl(std::declval<expected &>(), std::declval<F &&>()))
|
||||
TL_EXPECTED_11_CONSTEXPR decltype(expected_map_impl(
|
||||
std::declval<expected &>(), std::declval<F &&>()))
|
||||
map(F &&f) & {
|
||||
return expected_map_impl(*this, std::forward<F>(f));
|
||||
}
|
||||
template <class F>
|
||||
TL_EXPECTED_11_CONSTEXPR decltype(
|
||||
expected_map_impl(std::declval<expected>(), std::declval<F &&>()))
|
||||
TL_EXPECTED_11_CONSTEXPR decltype(expected_map_impl(std::declval<expected>(),
|
||||
std::declval<F &&>()))
|
||||
map(F &&f) && {
|
||||
return expected_map_impl(std::move(*this), std::forward<F>(f));
|
||||
}
|
||||
@@ -1373,14 +1376,14 @@ public:
|
||||
}
|
||||
#else
|
||||
template <class F>
|
||||
TL_EXPECTED_11_CONSTEXPR decltype(
|
||||
expected_map_impl(std::declval<expected &>(), std::declval<F &&>()))
|
||||
TL_EXPECTED_11_CONSTEXPR decltype(expected_map_impl(
|
||||
std::declval<expected &>(), std::declval<F &&>()))
|
||||
transform(F &&f) & {
|
||||
return expected_map_impl(*this, std::forward<F>(f));
|
||||
}
|
||||
template <class F>
|
||||
TL_EXPECTED_11_CONSTEXPR decltype(
|
||||
expected_map_impl(std::declval<expected>(), std::declval<F &&>()))
|
||||
TL_EXPECTED_11_CONSTEXPR decltype(expected_map_impl(std::declval<expected>(),
|
||||
std::declval<F &&>()))
|
||||
transform(F &&f) && {
|
||||
return expected_map_impl(std::move(*this), std::forward<F>(f));
|
||||
}
|
||||
@@ -2241,9 +2244,9 @@ template <class Exp, class F,
|
||||
detail::enable_if_t<!std::is_void<Ret>::value> * = nullptr>
|
||||
constexpr auto or_else_impl(Exp &&exp, F &&f) {
|
||||
static_assert(detail::is_expected<Ret>::value, "F must return an expected");
|
||||
return exp.has_value()
|
||||
? std::forward<Exp>(exp)
|
||||
: detail::invoke(std::forward<F>(f), std::forward<Exp>(exp).error());
|
||||
return exp.has_value() ? std::forward<Exp>(exp)
|
||||
: detail::invoke(std::forward<F>(f),
|
||||
std::forward<Exp>(exp).error());
|
||||
}
|
||||
|
||||
template <class Exp, class F,
|
||||
@@ -2251,9 +2254,9 @@ template <class Exp, class F,
|
||||
std::declval<Exp>().error())),
|
||||
detail::enable_if_t<std::is_void<Ret>::value> * = nullptr>
|
||||
detail::decay_t<Exp> or_else_impl(Exp &&exp, F &&f) {
|
||||
return exp.has_value()
|
||||
? std::forward<Exp>(exp)
|
||||
: (detail::invoke(std::forward<F>(f), std::forward<Exp>(exp).error()),
|
||||
return exp.has_value() ? std::forward<Exp>(exp)
|
||||
: (detail::invoke(std::forward<F>(f),
|
||||
std::forward<Exp>(exp).error()),
|
||||
std::forward<Exp>(exp));
|
||||
}
|
||||
#else
|
||||
@@ -2263,9 +2266,9 @@ template <class Exp, class F,
|
||||
detail::enable_if_t<!std::is_void<Ret>::value> * = nullptr>
|
||||
auto or_else_impl(Exp &&exp, F &&f) -> Ret {
|
||||
static_assert(detail::is_expected<Ret>::value, "F must return an expected");
|
||||
return exp.has_value()
|
||||
? std::forward<Exp>(exp)
|
||||
: detail::invoke(std::forward<F>(f), std::forward<Exp>(exp).error());
|
||||
return exp.has_value() ? std::forward<Exp>(exp)
|
||||
: detail::invoke(std::forward<F>(f),
|
||||
std::forward<Exp>(exp).error());
|
||||
}
|
||||
|
||||
template <class Exp, class F,
|
||||
@@ -2273,9 +2276,9 @@ template <class Exp, class F,
|
||||
std::declval<Exp>().error())),
|
||||
detail::enable_if_t<std::is_void<Ret>::value> * = nullptr>
|
||||
detail::decay_t<Exp> or_else_impl(Exp &&exp, F &&f) {
|
||||
return exp.has_value()
|
||||
? std::forward<Exp>(exp)
|
||||
: (detail::invoke(std::forward<F>(f), std::forward<Exp>(exp).error()),
|
||||
return exp.has_value() ? std::forward<Exp>(exp)
|
||||
: (detail::invoke(std::forward<F>(f),
|
||||
std::forward<Exp>(exp).error()),
|
||||
std::forward<Exp>(exp));
|
||||
}
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user