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 =
namespace detail {
namespace apply_tuple {
namespace adl {
using std::get;
struct ApplyInvoke {
template <std::size_t I>
struct invoke_get_fn {
template <typename T>
using seq = index_sequence_for_tuple<std::remove_reference_t<T>>;
template <typename F, typename T, std::size_t... I>
static constexpr auto
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))...);
constexpr auto operator()(T&& t) const
noexcept(noexcept(get<I>(static_cast<T&&>(t))))
-> decltype(get<I>(static_cast<T&&>(t))) {
return get<I>(static_cast<T&&>(t));
}
};
} // namespace adl
template <
typename Tuple,
std::size_t... Indices,
typename ReturnTuple =
std::tuple<decltype(get<Indices>(std::declval<Tuple>()))...>>
typename ReturnTuple = std::tuple<
decltype(adl::invoke_get_fn<Indices>{}(std::declval<Tuple>()))...>>
auto forward_tuple(Tuple&& tuple, std::index_sequence<Indices...>)
-> 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 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:
template <typename F, typename T>
constexpr auto operator()(F&& f, T&& t) const noexcept(
......@@ -116,12 +134,12 @@ constexpr decltype(auto) apply(F&& func, Tuple&& tuple) {
*/
template <typename Tuple>
auto forward_tuple(Tuple&& tuple) noexcept
-> decltype(detail::apply_tuple::adl::forward_tuple(
-> decltype(detail::apply_tuple::forward_tuple(
std::declval<Tuple>(),
std::declval<
index_sequence_for_tuple<std::remove_reference_t<Tuple>>>())) {
return detail::apply_tuple::adl::forward_tuple(
std::forward<Tuple>(tuple),
return detail::apply_tuple::forward_tuple(
static_cast<Tuple&&>(tuple),
index_sequence_for_tuple<std::remove_reference_t<Tuple>>{});
}
......@@ -129,30 +147,30 @@ auto forward_tuple(Tuple&& tuple) noexcept
* Mimic the invoke suite of traits for tuple based apply invocation
*/
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>
using apply_result_t = invoke_result_t<ApplyInvoke, F, 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 =
is_applicable<F, Tuple>::value;
template <typename R, typename F, typename Tuple>
struct is_applicable_r : is_invocable_r<R, ApplyInvoke, F, Tuple> {};
is_invocable_v<ApplyInvoke, F, Tuple>;
template <typename F, typename Tuple>
using is_applicable = is_invocable<ApplyInvoke, F, Tuple>;
template <typename R, typename F, typename Tuple>
FOLLY_INLINE_VARIABLE constexpr bool is_applicable_r_v =
is_applicable_r<R, F, Tuple>::value;
template <typename F, typename Tuple>
struct is_nothrow_applicable : is_nothrow_invocable<ApplyInvoke, F, Tuple> {};
is_invocable_r_v<R, ApplyInvoke, F, Tuple>;
template <typename R, typename F, typename Tuple>
using is_applicable_r = is_invocable_r<R, ApplyInvoke, F, Tuple>;
template <typename F, typename Tuple>
FOLLY_INLINE_VARIABLE constexpr bool is_nothrow_applicable_v =
is_nothrow_applicable<F, Tuple>::value;
template <typename R, typename F, typename Tuple>
struct is_nothrow_applicable_r
: is_nothrow_invocable_r<R, ApplyInvoke, F, Tuple> {};
is_nothrow_invocable_v<ApplyInvoke, F, Tuple>;
template <typename F, typename Tuple>
using is_nothrow_applicable = is_nothrow_invocable<ApplyInvoke, F, Tuple>;
template <typename R, typename F, typename Tuple>
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 apply_tuple {
......
This diff is collapsed.
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