Implement invoke_result_t and all dependencies

This commit is contained in:
BlackMark 2020-05-16 16:43:37 +02:00
parent 65d8928ff4
commit 01fad9046e

219
type.hpp
View File

@ -19,15 +19,6 @@ template <typename T> struct remove_reference<T &> { using type = T; };
template <typename T> struct remove_reference<T &&> { using type = T; };
template <typename T> using remove_reference_t = typename remove_reference<T>::type;
namespace detail {
template <typename T> struct type_identity { using type = T; };
template <typename T> auto try_add_pointer(int) -> type_identity<remove_reference_t<T> *>;
template <typename T> auto try_add_pointer(...) -> type_identity<T>;
} // namespace detail
template <typename T, T v>
struct integral_constant {
static constexpr T value = v;
@ -40,20 +31,6 @@ template <bool B> using bool_constant = integral_constant<bool, B>;
using false_type = bool_constant<false>;
using true_type = bool_constant<true>;
template <bool B, typename T, typename F> struct conditional { using type = T; };
template <typename T, typename F> struct conditional<false, T, F> { using type = F; };
template <bool B, class T, class F> using conditional_t = typename conditional<B, T, F>::type;
template <typename T> struct is_array : false_type {};
template <typename T> struct is_array<T[]> : true_type {};
template <typename T, size_t N> struct is_array<T[N]> : true_type {};
template <typename T> inline constexpr bool is_array_v = is_array<T>::value;
template <typename T> struct remove_extent { using type = T; };
template <typename T> struct remove_extent<T[]> { using type = T; };
template <typename T, size_t N> struct remove_extent<T[N]> { using type = T; };
template <typename T> using remove_extent_t = typename remove_extent<T>::type;
template <typename T> struct is_const : false_type {};
template <typename T> struct is_const<const T> : true_type {};
template <typename T> inline constexpr bool is_const_v = is_const<T>::value;
@ -70,9 +47,66 @@ template <typename T> inline constexpr bool is_lvalue_reference_v = is_lvalue_re
template <typename T> struct is_function : integral_constant<bool, !is_const_v<const T> && !is_reference_v<T>> {};
template <typename T> inline constexpr bool is_function_v = is_function<T>::value;
// Must use intrinsic, because this cannot be implemented in standard C++
template <typename T> struct is_union : integral_constant<bool, __is_union(T)> {};
template <typename T> inline constexpr bool is_union_v = is_union<T>::value;
namespace detail {
template <typename T> struct type_identity { using type = T; };
template <typename T> auto try_add_pointer(int) -> type_identity<remove_reference_t<T> *>;
template <typename T> auto try_add_pointer(...) -> type_identity<T>;
template <typename T> auto try_add_lvalue_reference(int) -> type_identity<T &>;
template <typename T> auto try_add_lvalue_reference(...) -> type_identity<T>;
template <typename T> auto try_add_rvalue_reference(int) -> type_identity<T &&>;
template <typename T> auto try_add_rvalue_reference(...) -> type_identity<T>;
template <typename T> struct is_pointer_helper : false_type {};
template <typename T> struct is_pointer_helper<T *> : true_type {};
template <typename T> struct is_member_pointer_helper : false_type {};
template <typename T, class U> struct is_member_pointer_helper<T U::*> : true_type {};
template <typename T> struct is_member_function_pointer_helper : false_type {};
template <typename T, typename U> struct is_member_function_pointer_helper<T U::*> : is_function<T> {};
template <typename T> integral_constant<bool, !is_union<T>::value> is_class_helper(int T::*);
template <typename> false_type is_class_helper(...);
template <typename B> true_type test_pre_ptr_convertible(const volatile B *);
template <typename> false_type test_pre_ptr_convertible(const volatile void *);
template <typename, typename> auto test_pre_is_base_of(...) -> true_type;
template <typename B, typename D> auto test_pre_is_base_of(int) -> decltype(test_pre_ptr_convertible<B>(static_cast<D *>(nullptr)));
} // namespace detail
template <bool B, typename T, typename F> struct conditional { using type = T; };
template <typename T, typename F> struct conditional<false, T, F> { using type = F; };
template <bool B, class T, class F> using conditional_t = typename conditional<B, T, F>::type;
template <typename T> struct is_array : false_type {};
template <typename T> struct is_array<T[]> : true_type {};
template <typename T, size_t N> struct is_array<T[N]> : true_type {};
template <typename T> inline constexpr bool is_array_v = is_array<T>::value;
template <typename T> struct remove_extent { using type = T; };
template <typename T> struct remove_extent<T[]> { using type = T; };
template <typename T, size_t N> struct remove_extent<T[N]> { using type = T; };
template <typename T> using remove_extent_t = typename remove_extent<T>::type;
template <typename T> struct add_pointer : decltype(detail::try_add_pointer<T>(0)) {};
template <typename T> using add_pointer_t = typename add_pointer<T>::type;
template <typename T> struct add_lvalue_reference : decltype(detail::try_add_lvalue_reference<T>(0)) {};
template <typename T> using add_lvalue_reference_t = typename add_lvalue_reference<T>::type;
template <typename T> struct add_rvalue_reference : decltype(detail::try_add_rvalue_reference<T>(0)) {};
template <typename T> using add_rvalue_reference_t = typename add_rvalue_reference<T>::type;
template <typename T> struct remove_cv { using type = T; };
template <typename T> struct remove_cv<const T> { using type = T; };
template <typename T> struct remove_cv<volatile T> { using type = T; };
@ -85,6 +119,9 @@ template <typename T> using remove_cv_t = typename remove_cv<T>::type;
template <typename T> using remove_const_t = typename remove_const<T>::type;
template <typename T> using remove_volatile_t = typename remove_volatile<T>::type;
template <typename T> struct remove_cvref { using type = remove_cv_t<remove_reference_t<T>>; };
template <typename T> using remove_cvref_t = typename remove_cvref<T>::type;
template <typename T>
struct decay {
private:
@ -132,6 +169,142 @@ struct is_integral : integral_constant<bool,
{};
template <typename T> inline constexpr auto is_integral_v = is_integral<T>::value;
template <typename T> struct is_arithmetic : integral_constant<bool, is_integral<T>::value || is_floating_point<T>::value> {};
template <typename T> inline constexpr bool is_arithmetic_v = is_arithmetic<T>::value;
template <typename T> struct is_void : is_same<void, remove_cv_t<T>> {};
template <typename T> inline constexpr bool is_void_v = is_void<T>::value;
template <typename T> struct is_pointer : detail::is_pointer_helper<remove_cv_t<T>> {};
template <typename T> inline constexpr bool is_pointer_v = is_pointer<T>::value;
template <typename T> struct is_member_pointer : detail::is_member_pointer_helper<remove_cv_t<T>> {};
template <typename T> inline constexpr bool is_member_pointer_v = is_member_pointer<T>::value;
template <typename T> struct is_member_function_pointer : detail::is_member_function_pointer_helper<remove_cv_t<T>> {};
template <typename T> inline constexpr bool is_member_function_pointer_v = is_member_function_pointer<T>::value;
template <typename T> struct is_member_object_pointer : integral_constant<bool, is_member_pointer_v<T> && !is_member_function_pointer_v<T>> {};
template <typename T> inline constexpr bool is_member_object_pointer_v = is_member_object_pointer<T>::value;
template <typename T> struct is_class : decltype(detail::is_class_helper<T>(nullptr)) {};
template <typename T> inline constexpr bool is_class_v = is_class<T>::value;
template <typename T> struct is_enum : integral_constant<bool,
!is_void_v<T> &&
!is_integral_v<T> &&
!is_floating_point_v<T> &&
!is_array_v<T> &&
!is_pointer_v<T> &&
!is_reference_v<T> &&
!is_member_pointer_v<T> &&
!is_union_v<T> &&
!is_class_v<T> &&
!is_function_v<T>>
{};
template <typename T> inline constexpr bool is_enum_v = is_enum<T>::value;
template <typename T> struct is_null_pointer : is_same<nullptr_t, remove_cv_t<T>> {};
template <typename T> inline constexpr bool is_null_pointer_v = is_null_pointer<T>::value;
template <typename T>
struct is_scalar : integral_constant<bool,
is_arithmetic_v<T> ||
is_enum_v<T> ||
is_pointer_v<T> ||
is_member_pointer_v<T> ||
is_null_pointer_v<T>>
{};
template <typename T> inline constexpr bool is_scalar_v = is_scalar<T>::value;
template <typename T> struct is_object : integral_constant<bool,
is_scalar_v<T> ||
is_array_v<T> ||
is_union_v<T> ||
is_class_v<T>>
{};
template <typename T> inline constexpr bool is_object_v = is_object<T>::value;
template <typename Base, typename Derived> struct is_base_of : integral_constant<bool,
is_class_v<Base> &&
is_class_v<Derived> &&
decltype(detail::test_pre_is_base_of<Base, Derived>(0))::value>
{};
template <typename Base, typename Derived> inline constexpr bool is_base_of_v = is_base_of<Base, Derived>::value;
template <bool B, typename T = void> struct enable_if {};
template <typename T> struct enable_if<true, T> { using type = T; };
template <bool B, typename T = void> using enable_if_t = typename enable_if<B, T>::type;
} // namespace type
//////////////////////////////////////////////////////////////////////////
// Forward declarations
namespace util {
template <typename T> struct reference_wrapper;
template <typename T> inline constexpr T &&forward(type::remove_reference_t<T> &t) noexcept;
template <typename T> type::add_rvalue_reference_t<T> declval() noexcept;
} // namespace util
//////////////////////////////////////////////////////////////////////////
namespace type {
// Everything is nothrow_invocable, because exceptions are disabled in avr-gcc
template <typename, typename...> struct is_nothrow_invocable : true_type {};
template <typename Fn, typename... Args> inline constexpr auto is_nothrow_invocable_v = is_nothrow_invocable<Fn, Args...>::value;
namespace detail {
template <typename T> struct is_reference_wrapper : false_type {};
template <typename U> struct is_reference_wrapper<util::reference_wrapper<U>> : true_type {};
template <typename T>
struct invoke_impl {
template <typename Fn, typename... Args>
static auto call(Fn &&f, Args &&... args) -> decltype(util::forward<Fn>(f)(util::forward<Args>(args)...));
};
template <typename B, typename MT>
struct invoke_impl<MT B::*> {
template <typename T, typename Td = decay_t<T>, typename = enable_if_t<is_base_of_v<B, Td>>>
static auto get(T &&t) -> T &&;
template <typename T, typename Td = decay_t<T>, typename = enable_if_t<is_reference_wrapper<Td>::value>>
static auto get(T &&t) -> decltype(t.get());
template <typename T, typename Td = decay_t<T>,
typename = enable_if_t<!is_base_of_v<B, Td>>,
typename = enable_if_t<!is_reference_wrapper<Td>::value>>
static auto get(T &&t) -> decltype(*util::forward<T>(t));
template <typename T, typename... Args, typename MT1,
typename = enable_if_t<is_function_v<MT1>>>
static auto call(MT1 B::*pmf, T &&t, Args &&... args)
-> decltype((invoke_impl::get(util::forward<T>(t)).*pmf)(util::forward<Args>(args)...));
template <typename T>
static auto call(MT B::*pmd, T &&t) -> decltype(invoke_impl::get(util::forward<T>(t)).*pmd);
};
template <typename Fn, class... Args, class Fd = decay_t<Fn>>
auto INVOKE(Fn &&f, Args &&... args)
-> decltype(invoke_impl<Fd>::call(util::forward<Fn>(f), util::forward<Args>(args)...));
template <typename AlwaysVoid, typename, typename...> struct invoke_result {};
template <typename Fn, typename... Args>
struct invoke_result<decltype(void(INVOKE(util::declval<Fn>(), util::declval<Args>()...))), Fn, Args...> {
using type = decltype(INVOKE(util::declval<Fn>(), util::declval<Args>()...));
};
} // namespace detail
template <typename Fn, typename... ArgTypes> struct invoke_result : detail::invoke_result<void, Fn, ArgTypes...> {};
template <typename Fn, typename... ArgTypes> using invoke_result_t = typename invoke_result<Fn, ArgTypes...>::type;
// Not part of <type_traits>, but very useful
template <typename...> struct always_false : false_type {};
template <typename... Ts> inline constexpr auto always_false_v = always_false<Ts...>::value;