Commit 4bf029a2 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

Revise invoke and invocability traits

Summary:
[Folly] Revise invoke to be an invoker rather than a function, and therefore insusceptible to ADL, and revise invocability traits for compile-time perf.

Do the same for apply and applicability traits.

Reviewed By: Mizuchi

Differential Revision: D23334895

fbshipit-source-id: 4d3241a3f34a7bede44f8ab54a30a3074d9fd3ed
parent 533d54f5
...@@ -37,36 +37,54 @@ using index_sequence_for_tuple = ...@@ -37,36 +37,54 @@ using index_sequence_for_tuple =
namespace detail { namespace detail {
namespace apply_tuple { namespace apply_tuple {
namespace adl { namespace adl {
using std::get; using std::get;
struct ApplyInvoke { template <std::size_t I>
struct invoke_get_fn {
template <typename T> template <typename T>
using seq = index_sequence_for_tuple<std::remove_reference_t<T>>; constexpr auto operator()(T&& t) const
noexcept(noexcept(get<I>(static_cast<T&&>(t))))
template <typename F, typename T, std::size_t... I> -> decltype(get<I>(static_cast<T&&>(t))) {
static constexpr auto return get<I>(static_cast<T&&>(t));
invoke_(F&& f, T&& t, std::index_sequence<I...>) noexcept(
is_nothrow_invocable<F&&, decltype(get<I>(std::declval<T>()))...>::value)
-> invoke_result_t<F&&, decltype(get<I>(std::declval<T>()))...> {
return invoke(static_cast<F&&>(f), get<I>(static_cast<T&&>(t))...);
} }
}; };
} // namespace adl
template < template <
typename Tuple, typename Tuple,
std::size_t... Indices, std::size_t... Indices,
typename ReturnTuple = typename ReturnTuple = std::tuple<
std::tuple<decltype(get<Indices>(std::declval<Tuple>()))...>> decltype(adl::invoke_get_fn<Indices>{}(std::declval<Tuple>()))...>>
auto forward_tuple(Tuple&& tuple, std::index_sequence<Indices...>) auto forward_tuple(Tuple&& tuple, std::index_sequence<Indices...>)
-> ReturnTuple { -> ReturnTuple {
return ReturnTuple{get<Indices>(std::forward<Tuple>(tuple))...}; return ReturnTuple{
adl::invoke_get_fn<Indices>{}(static_cast<Tuple&&>(tuple))...};
} }
} // namespace adl
} // namespace apply_tuple } // namespace apply_tuple
} // namespace detail } // namespace detail
struct ApplyInvoke : private detail::apply_tuple::adl::ApplyInvoke { struct ApplyInvoke {
private:
template <typename T>
using seq = index_sequence_for_tuple<std::remove_reference_t<T>>;
template <std::size_t I>
using get = detail::apply_tuple::adl::invoke_get_fn<I>;
template <typename F, typename T, std::size_t... I>
static constexpr auto
invoke_(F&& f, T&& t, std::index_sequence<I...>) noexcept(
noexcept(invoke(static_cast<F&&>(f), get<I>{}(static_cast<T&&>(t))...)))
-> decltype(
invoke(static_cast<F&&>(f), get<I>{}(static_cast<T&&>(t))...)) {
return invoke(static_cast<F&&>(f), get<I>{}(static_cast<T&&>(t))...);
}
public: public:
template <typename F, typename T> template <typename F, typename T>
constexpr auto operator()(F&& f, T&& t) const noexcept( constexpr auto operator()(F&& f, T&& t) const noexcept(
...@@ -116,12 +134,12 @@ constexpr decltype(auto) apply(F&& func, Tuple&& tuple) { ...@@ -116,12 +134,12 @@ constexpr decltype(auto) apply(F&& func, Tuple&& tuple) {
*/ */
template <typename Tuple> template <typename Tuple>
auto forward_tuple(Tuple&& tuple) noexcept auto forward_tuple(Tuple&& tuple) noexcept
-> decltype(detail::apply_tuple::adl::forward_tuple( -> decltype(detail::apply_tuple::forward_tuple(
std::declval<Tuple>(), std::declval<Tuple>(),
std::declval< std::declval<
index_sequence_for_tuple<std::remove_reference_t<Tuple>>>())) { index_sequence_for_tuple<std::remove_reference_t<Tuple>>>())) {
return detail::apply_tuple::adl::forward_tuple( return detail::apply_tuple::forward_tuple(
std::forward<Tuple>(tuple), static_cast<Tuple&&>(tuple),
index_sequence_for_tuple<std::remove_reference_t<Tuple>>{}); index_sequence_for_tuple<std::remove_reference_t<Tuple>>{});
} }
...@@ -129,30 +147,30 @@ auto forward_tuple(Tuple&& tuple) noexcept ...@@ -129,30 +147,30 @@ auto forward_tuple(Tuple&& tuple) noexcept
* Mimic the invoke suite of traits for tuple based apply invocation * Mimic the invoke suite of traits for tuple based apply invocation
*/ */
template <typename F, typename Tuple> template <typename F, typename Tuple>
struct apply_result : invoke_result<ApplyInvoke, F, Tuple> {}; using apply_result = invoke_result<ApplyInvoke, F, Tuple>;
template <typename F, typename Tuple> template <typename F, typename Tuple>
using apply_result_t = invoke_result_t<ApplyInvoke, F, Tuple>; using apply_result_t = invoke_result_t<ApplyInvoke, F, Tuple>;
template <typename F, typename Tuple> template <typename F, typename Tuple>
struct is_applicable : is_invocable<ApplyInvoke, F, Tuple> {};
template <typename F, typename Tuple>
FOLLY_INLINE_VARIABLE constexpr bool is_applicable_v = FOLLY_INLINE_VARIABLE constexpr bool is_applicable_v =
is_applicable<F, Tuple>::value; is_invocable_v<ApplyInvoke, F, Tuple>;
template <typename R, typename F, typename Tuple> template <typename F, typename Tuple>
struct is_applicable_r : is_invocable_r<R, ApplyInvoke, F, Tuple> {}; using is_applicable = is_invocable<ApplyInvoke, F, Tuple>;
template <typename R, typename F, typename Tuple> template <typename R, typename F, typename Tuple>
FOLLY_INLINE_VARIABLE constexpr bool is_applicable_r_v = FOLLY_INLINE_VARIABLE constexpr bool is_applicable_r_v =
is_applicable_r<R, F, Tuple>::value; is_invocable_r_v<R, ApplyInvoke, F, Tuple>;
template <typename F, typename Tuple> template <typename R, typename F, typename Tuple>
struct is_nothrow_applicable : is_nothrow_invocable<ApplyInvoke, F, Tuple> {}; using is_applicable_r = is_invocable_r<R, ApplyInvoke, F, Tuple>;
template <typename F, typename Tuple> template <typename F, typename Tuple>
FOLLY_INLINE_VARIABLE constexpr bool is_nothrow_applicable_v = FOLLY_INLINE_VARIABLE constexpr bool is_nothrow_applicable_v =
is_nothrow_applicable<F, Tuple>::value; is_nothrow_invocable_v<ApplyInvoke, F, Tuple>;
template <typename R, typename F, typename Tuple> template <typename F, typename Tuple>
struct is_nothrow_applicable_r using is_nothrow_applicable = is_nothrow_invocable<ApplyInvoke, F, Tuple>;
: is_nothrow_invocable_r<R, ApplyInvoke, F, Tuple> {};
template <typename R, typename F, typename Tuple> template <typename R, typename F, typename Tuple>
FOLLY_INLINE_VARIABLE constexpr bool is_nothrow_applicable_r_v = FOLLY_INLINE_VARIABLE constexpr bool is_nothrow_applicable_r_v =
is_nothrow_applicable_r<R, F, Tuple>::value; is_nothrow_invocable_r_v<R, ApplyInvoke, F, Tuple>;
template <typename R, typename F, typename Tuple>
using is_nothrow_applicable_r =
is_nothrow_invocable_r<R, ApplyInvoke, F, Tuple>;
namespace detail { namespace detail {
namespace apply_tuple { namespace apply_tuple {
......
...@@ -47,159 +47,158 @@ ...@@ -47,159 +47,158 @@
* * std::is_nothrow_invocable_r_v * * std::is_nothrow_invocable_r_v
*/ */
#if __cpp_lib_invoke >= 201411 || _MSC_VER
namespace folly {
/* using override */ using std::invoke;
}
#else
namespace folly { namespace folly {
// invoke_fn
// invoke
//
// mimic: std::invoke, C++17 // mimic: std::invoke, C++17
template <typename F, typename... Args> struct invoke_fn {
FOLLY_ERASE constexpr auto invoke(F&& f, Args&&... args) noexcept( template <typename F, typename... A>
noexcept(static_cast<F&&>(f)(static_cast<Args&&>(args)...))) FOLLY_ERASE constexpr auto operator()(F&& f, A&&... a) const
-> decltype(static_cast<F&&>(f)(static_cast<Args&&>(args)...)) { noexcept(noexcept(static_cast<F&&>(f)(static_cast<A&&>(a)...)))
return static_cast<F&&>(f)(static_cast<Args&&>(args)...); -> decltype(static_cast<F&&>(f)(static_cast<A&&>(a)...)) {
} return static_cast<F&&>(f)(static_cast<A&&>(a)...);
template <typename M, typename C, typename... Args> }
FOLLY_ERASE constexpr auto invoke(M(C::*d), Args&&... args) template <typename M, typename C, typename... A>
-> decltype(std::mem_fn(d)(static_cast<Args&&>(args)...)) { FOLLY_ERASE constexpr auto operator()(M C::*f, A&&... a) const
return std::mem_fn(d)(static_cast<Args&&>(args)...); noexcept(noexcept(std::mem_fn(f)(static_cast<A&&>(a)...)))
} -> decltype(std::mem_fn(f)(static_cast<A&&>(a)...)) {
return std::mem_fn(f)(static_cast<A&&>(a)...);
} // namespace folly }
};
#endif
// Only available in >= MSVC 2017 15.3 in C++17
#if __cpp_lib_is_invocable >= 201703 || \
(_MSC_VER >= 1911 && _MSVC_LANG > 201402)
namespace folly {
/* using override */ using std::invoke_result; FOLLY_INLINE_VARIABLE constexpr invoke_fn invoke;
/* using override */ using std::invoke_result_t;
/* using override */ using std::is_invocable;
/* using override */ using std::is_invocable_r;
/* using override */ using std::is_invocable_r_v;
/* using override */ using std::is_invocable_v;
/* using override */ using std::is_nothrow_invocable;
/* using override */ using std::is_nothrow_invocable_r;
/* using override */ using std::is_nothrow_invocable_r_v;
/* using override */ using std::is_nothrow_invocable_v;
} // namespace folly } // namespace folly
#else
namespace folly { namespace folly {
namespace invoke_detail { namespace invoke_detail {
template <typename F, typename... Args> template <typename F>
using invoke_result_ = struct traits {
decltype(invoke(std::declval<F>(), std::declval<Args>()...)); template <typename... A>
using result = decltype( //
template <typename F, typename... Args> FOLLY_DECLVAL(F &&)(FOLLY_DECLVAL(A &&)...));
struct invoke_nothrow_ template <typename... A>
: bool_constant<noexcept( static constexpr bool nothrow = noexcept( //
invoke(std::declval<F>(), std::declval<Args>()...))> {}; FOLLY_DECLVAL(F&&)(FOLLY_DECLVAL(A&&)...));
};
// from: http://en.cppreference.com/w/cpp/types/result_of, CC-BY-SA template <typename P>
struct traits_member_ptr {
template <typename Void, typename F, typename... Args> template <typename... A>
using result = decltype( //
std::mem_fn(FOLLY_DECLVAL(P))(FOLLY_DECLVAL(A &&)...));
template <typename... A>
static constexpr bool nothrow = noexcept( //
std::mem_fn(FOLLY_DECLVAL(P))(FOLLY_DECLVAL(A&&)...));
};
template <typename M, typename C>
struct traits<M C::*> : traits_member_ptr<M C::*> {};
template <typename M, typename C>
struct traits<M C::*const> : traits_member_ptr<M C::*> {};
template <typename M, typename C>
struct traits<M C::*&> : traits_member_ptr<M C::*> {};
template <typename M, typename C>
struct traits<M C::*const&> : traits_member_ptr<M C::*> {};
template <typename M, typename C>
struct traits<M C::*&&> : traits_member_ptr<M C::*> {};
template <typename M, typename C>
struct traits<M C::*const&&> : traits_member_ptr<M C::*> {};
// adapted from: http://en.cppreference.com/w/cpp/types/result_of, CC-BY-SA
template <typename F, typename... A>
using invoke_result_t = typename traits<F>::template result<A...>;
template <typename Void, typename F, typename... A>
struct invoke_result {}; struct invoke_result {};
template <typename F, typename... Args> template <typename F, typename... A>
struct invoke_result<void_t<invoke_result_<F, Args...>>, F, Args...> { struct invoke_result<void_t<invoke_result_t<F, A...>>, F, A...> {
using type = invoke_result_<F, Args...>; using type = invoke_result_t<F, A...>;
}; };
template <typename Void, typename F, typename... Args> template <typename Void, typename F, typename... A>
struct is_invocable : std::false_type {}; FOLLY_INLINE_VARIABLE constexpr bool is_invocable_v = false;
template <typename F, typename... Args> template <typename F, typename... A>
struct is_invocable<void_t<invoke_result_<F, Args...>>, F, Args...> FOLLY_INLINE_VARIABLE constexpr bool
: std::true_type {}; is_invocable_v<void_t<invoke_result_t<F, A...>>, F, A...> = true;
template <typename Void, typename R, typename F, typename... Args> template <typename Void, typename R, typename F, typename... A>
struct is_invocable_r : std::false_type {}; FOLLY_INLINE_VARIABLE constexpr bool is_invocable_r_v = false;
template <typename R, typename F, typename... Args> template <typename R, typename F, typename... A>
struct is_invocable_r<void_t<invoke_result_<F, Args...>>, R, F, Args...> FOLLY_INLINE_VARIABLE constexpr bool
: std::is_convertible<invoke_result_<F, Args...>, R> {}; is_invocable_r_v<void_t<invoke_result_t<F, A...>>, R, F, A...> =
std::is_convertible<invoke_result_t<F, A...>, R>::value;
template <typename Void, typename F, typename... Args> template <typename Void, typename F, typename... A>
struct is_nothrow_invocable : std::false_type {}; FOLLY_INLINE_VARIABLE constexpr bool is_nothrow_invocable_v = false;
template <typename F, typename... Args> template <typename F, typename... A>
struct is_nothrow_invocable<void_t<invoke_result_<F, Args...>>, F, Args...> FOLLY_INLINE_VARIABLE constexpr bool
: invoke_nothrow_<F, Args...> {}; is_nothrow_invocable_v<void_t<invoke_result_t<F, A...>>, F, A...> =
traits<F>::template nothrow<A...>;
template <typename Void, typename R, typename F, typename... Args> template <typename Void, typename R, typename F, typename... A>
struct is_nothrow_invocable_r : std::false_type {}; FOLLY_INLINE_VARIABLE constexpr bool is_nothrow_invocable_r_v = false;
template <typename R, typename F, typename... Args> template <typename R, typename F, typename... A>
struct is_nothrow_invocable_r<void_t<invoke_result_<F, Args...>>, R, F, Args...> FOLLY_INLINE_VARIABLE constexpr bool
: StrictConjunction< is_nothrow_invocable_r_v<void_t<invoke_result_t<F, A...>>, R, F, A...> =
std::is_convertible<invoke_result_<F, Args...>, R>, std::is_convertible<invoke_result_t<F, A...>, R>::value&&
invoke_nothrow_<F, Args...>> {}; traits<F>::template nothrow<A...>;
} // namespace invoke_detail } // namespace invoke_detail
// mimic: std::invoke_result, C++17 // mimic: std::invoke_result, C++17
template <typename F, typename... Args> template <typename F, typename... A>
struct invoke_result : invoke_detail::invoke_result<void, F, Args...> {}; using invoke_result = invoke_detail::invoke_result<void, F, A...>;
// mimic: std::invoke_result_t, C++17 // mimic: std::invoke_result_t, C++17
template <typename F, typename... Args> using invoke_detail::invoke_result_t;
using invoke_result_t = typename invoke_result<F, Args...>::type;
// mimic: std::is_invocable, C++17
template <typename F, typename... Args>
struct is_invocable : invoke_detail::is_invocable<void, F, Args...> {};
// mimic: std::is_invocable_v, C++17 // mimic: std::is_invocable_v, C++17
template <typename F, typename... Args> template <typename F, typename... A>
FOLLY_INLINE_VARIABLE constexpr bool is_invocable_v = FOLLY_INLINE_VARIABLE constexpr bool is_invocable_v =
is_invocable<F, Args...>::value; invoke_detail::is_invocable_v<void, F, A...>;
// mimic: std::is_invocable_r, C++17 // mimic: std::is_invocable, C++17
template <typename R, typename F, typename... Args> template <typename F, typename... A>
struct is_invocable_r : invoke_detail::is_invocable_r<void, R, F, Args...> {}; struct is_invocable : bool_constant<is_invocable_v<F, A...>> {};
// mimic: std::is_invocable_r_v, C++17 // mimic: std::is_invocable_r_v, C++17
template <typename R, typename F, typename... Args> template <typename R, typename F, typename... A>
FOLLY_INLINE_VARIABLE constexpr bool is_invocable_r_v = FOLLY_INLINE_VARIABLE constexpr bool is_invocable_r_v =
is_invocable_r<R, F, Args...>::value; invoke_detail::is_invocable_r_v<void, R, F, A...>;
// mimic: std::is_nothrow_invocable, C++17 // mimic: std::is_invocable_r, C++17
template <typename F, typename... Args> template <typename R, typename F, typename... A>
struct is_nothrow_invocable struct is_invocable_r : bool_constant<is_invocable_r_v<R, F, A...>> {};
: invoke_detail::is_nothrow_invocable<void, F, Args...> {};
// mimic: std::is_nothrow_invocable_v, C++17 // mimic: std::is_nothrow_invocable_v, C++17
template <typename F, typename... Args> template <typename F, typename... A>
FOLLY_INLINE_VARIABLE constexpr bool is_nothrow_invocable_v = FOLLY_INLINE_VARIABLE constexpr bool is_nothrow_invocable_v =
is_nothrow_invocable<F, Args...>::value; invoke_detail::is_nothrow_invocable_v<void, F, A...>;
// mimic: std::is_nothrow_invocable_r, C++17 // mimic: std::is_nothrow_invocable, C++17
template <typename R, typename F, typename... Args> template <typename F, typename... A>
struct is_nothrow_invocable_r struct is_nothrow_invocable : bool_constant<is_nothrow_invocable_v<F, A...>> {};
: invoke_detail::is_nothrow_invocable_r<void, R, F, Args...> {};
// mimic: std::is_nothrow_invocable_r_v, C++17 // mimic: std::is_nothrow_invocable_r_v, C++17
template <typename R, typename F, typename... Args> template <typename R, typename F, typename... Args>
FOLLY_INLINE_VARIABLE constexpr bool is_nothrow_invocable_r_v = FOLLY_INLINE_VARIABLE constexpr bool is_nothrow_invocable_r_v =
is_nothrow_invocable_r<R, F, Args...>::value; invoke_detail::is_nothrow_invocable_r_v<void, R, F, Args...>;
} // namespace folly // mimic: std::is_nothrow_invocable_r, C++17
template <typename R, typename F, typename... A>
struct is_nothrow_invocable_r
: bool_constant<is_nothrow_invocable_r_v<R, F, A...>> {};
#endif } // namespace folly
namespace folly { namespace folly {
...@@ -207,17 +206,17 @@ namespace detail { ...@@ -207,17 +206,17 @@ namespace detail {
struct invoke_private_overload; struct invoke_private_overload;
template <bool, typename Invoke> template <bool, typename I>
struct invoke_traits_base_ {}; struct invoke_traits_base_ {};
template <typename Invoke> template <typename I>
struct invoke_traits_base_<false, Invoke> {}; struct invoke_traits_base_<false, I> {};
template <typename Invoke> template <typename I>
struct invoke_traits_base_<true, Invoke> { struct invoke_traits_base_<true, I> {
FOLLY_INLINE_VARIABLE static constexpr Invoke invoke{}; FOLLY_INLINE_VARIABLE static constexpr I invoke{};
}; };
template <typename Invoke> template <typename I>
using invoke_traits_base = using invoke_traits_base =
invoke_traits_base_<is_constexpr_default_constructible_v<Invoke>, Invoke>; invoke_traits_base_<is_constexpr_default_constructible_v<I>, I>;
} // namespace detail } // namespace detail
...@@ -249,40 +248,44 @@ using invoke_traits_base = ...@@ -249,40 +248,44 @@ using invoke_traits_base =
// if the latter is constexpr default-constructible: // if the latter is constexpr default-constructible:
// //
// * invoke // * invoke
template <typename Invoke> template <typename I>
struct invoke_traits : detail::invoke_traits_base<Invoke> { struct invoke_traits : detail::invoke_traits_base<I> {
public: public:
using invoke_type = Invoke; using invoke_type = I;
// If invoke_type is constexpr default-constructible: // If invoke_type is constexpr default-constructible:
// //
// inline static constexpr invoke_type invoke{}; // inline static constexpr invoke_type invoke{};
template <typename... Args> template <typename... A>
struct invoke_result : folly::invoke_result<Invoke, Args...> {}; using invoke_result = invoke_detail::invoke_result<void, I, A...>;
template <typename... Args> template <typename... A>
using invoke_result_t = folly::invoke_result_t<Invoke, Args...>; using invoke_result_t = invoke_detail::invoke_result_t<I, A...>;
template <typename... Args> template <typename... A>
struct is_invocable : folly::is_invocable<Invoke, Args...> {};
template <typename... Args>
FOLLY_INLINE_VARIABLE static constexpr bool is_invocable_v = FOLLY_INLINE_VARIABLE static constexpr bool is_invocable_v =
is_invocable<Args...>::value; invoke_detail::is_invocable_v<void, I, A...>;
template <typename R, typename... Args> template <typename... A>
struct is_invocable_r : folly::is_invocable_r<R, Invoke, Args...> {}; struct is_invocable //
template <typename R, typename... Args> : bool_constant<invoke_detail::is_invocable_v<void, I, A...>> {};
template <typename R, typename... A>
FOLLY_INLINE_VARIABLE static constexpr bool is_invocable_r_v = FOLLY_INLINE_VARIABLE static constexpr bool is_invocable_r_v =
is_invocable_r<R, Args...>::value; invoke_detail::is_invocable_r_v<void, R, I, A...>;
template <typename... Args> template <typename R, typename... A>
struct is_nothrow_invocable : folly::is_nothrow_invocable<Invoke, Args...> {}; struct is_invocable_r //
template <typename... Args> : bool_constant<invoke_detail::is_invocable_r_v<void, R, I, A...>> {};
template <typename... A>
FOLLY_INLINE_VARIABLE static constexpr bool is_nothrow_invocable_v = FOLLY_INLINE_VARIABLE static constexpr bool is_nothrow_invocable_v =
is_nothrow_invocable<Args...>::value; invoke_detail::is_nothrow_invocable_v<void, I, A...>;
template <typename R, typename... Args> template <typename... A>
struct is_nothrow_invocable_r struct is_nothrow_invocable //
: folly::is_nothrow_invocable_r<R, Invoke, Args...> {}; : bool_constant<invoke_detail::is_nothrow_invocable_v<void, I, A...>> {};
template <typename R, typename... Args> template <typename R, typename... A>
FOLLY_INLINE_VARIABLE static constexpr bool is_nothrow_invocable_r_v = FOLLY_INLINE_VARIABLE static constexpr bool is_nothrow_invocable_r_v =
is_nothrow_invocable_r<R, Args...>::value; invoke_detail::is_nothrow_invocable_r_v<void, R, I, A...>;
template <typename R, typename... A>
struct is_nothrow_invocable_r //
: bool_constant<
invoke_detail::is_nothrow_invocable_r_v<void, R, I, A...>> {};
}; };
} // namespace folly } // namespace folly
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment