How do I pass the function name as a compile-time parameter?

So far, I have been able to determine if a function exists in a class by name, without having to sign it, as follows:

Following a snippet of code "inspired" by the answer to this question: Is it possible to define the parameter type and return type of a lambda?

I understand that different specializations must be created for different modifiers such as member functions.

template <typename T, typename TReturn, typename... Args>
struct function_traits<TReturn( T::* )( Args... )>
{
    using return_type = TReturn;
    using arguments = std::tuple<Args...>;

    static std::size_t constexpr arg_count = sizeof...( Args );

    template <std::size_t N, typename = void>
    struct argument
    {
        using type = std::tuple_element_t<N, arguments>;
    };

    template <std::size_t N>
    struct argument<N, std::enable_if_t<( !( N < arg_count ) ), void>> {};
};

      

And my own code as a workaround for 28.4.4 of The C ++ Programming Language, 4th edition since I am using VC ++ 2015 >.

template <typename T>
struct has_func_f
{
private:
    template <typename C, typename = void>
    struct get_result
    {
        static bool constexpr value = false;
    };

    // need <function_name> as a class parameter so I can append and get &C::<function_name>
    template <typename C>
    struct get_result<C, std::enable_if_t<( std::is_same<decltype( ( decltype( std::declval<typename function_traits<decltype( &C::f )>::arguments>() ) ) ( std::declval<typename function_traits<decltype( &C::f )>::arguments>() ), std::true_type() ), std::true_type>::value ), void>>
    {
        static bool constexpr value = true;
    };

public:
    static bool constexpr value = get_result<T>::value;
};

      

This code works to detect any function signature as long as the name matches. My use case:

struct A {};
struct B { float f(int, double); };
struct C { void f(); };

int main()
{
    has_func_f<A>::value; // false
    has_func_f<B>::value; // true
    has_func_f<C>::value; // true

    return 0;
}

      

So my question says how can I do what I want? I looked and looked, and the closest I came to is to use a macro to create an appropriate structure for the function name I want. Is there another way?

+3


source to share





All Articles