How can we modify the discovery toolkit to check if a class has a member function with a specific signature?

Given the (reduced) implementation of the discovery idiom

namespace type_traits
{
    template<typename... Ts>
    using void_t = void;

    namespace detail
    {
        template<typename, template<typename...> class, typename...>
        struct is_detected : std::false_type {};

        template<template<class...> class Operation, typename... Arguments>
        struct is_detected<void_t<Operation<Arguments...>>, Operation, Arguments...> : std::true_type {};
    }

    template<template<class...> class Operation, typename... Arguments>
    using is_detected = detail::is_detected<void_t<>, Operation, Arguments...>;

    template<template<class...> class Operation, typename... Arguments>
    constexpr bool is_detected_v = detail::is_detected<void_t<>, Operation, Arguments...>::value;
}

we can check if the class foocontains a member functionbar

struct  foo {
    int const& bar(int&&) { return 0; }
};

template<class T, typename... Arguments>
using bar_t = std::conditional_t<
    true,
    decltype(std::declval<T>().bar(std::declval<Arguments>()...)),
    std::integral_constant<
        decltype(std::declval<T>().bar(std::declval<Arguments>()...)) (T::*)(Arguments...),
        &T::bar
    >
>;

int main()
{
    static_assert(type_traits::is_detected_v<bar_t, foo, int&&>, "not detected");
    return 0;
}

However, as you can see, we need to write “complex code” for each method that we want to detect.

Is it possible to encapsulate an idea bar_tin a more general test? I would like to provide the operation foo::barand type of the required argument and run a test, for example static_assert(type_traits::is_detected_v<bar_t, foo, int&&>, "not detected");.

[I created a live demo of this example.]

+4

All Articles