Search code examples
c++templatesfunction-pointerspointer-to-member

C++ deduce member function parameters


I'm looking to extend the functionality described here but for member functions, what would be the syntax in this case?

Also, the (*) in the template definition, is that de-referencing the function pointer so the compiler can deduce the template arguments?

Would appreciate any input!

Thanks

template <class F> struct ArgType;

template <class R, class T> 
struct ArgType<R(*)(T)> {
  typedef T type;
}; 

void f(int) {}

#include <type_traits>
#include <iostream>

int main() {

  // To prove
  std::cout << std::is_same< ArgType<decltype(&f)>::type, int >::value << '\n';

  // To use
  ArgType<decltype(&f)>::type a;
}

Solution

  • Pointer-to-members look like Ret (Cls::*)(Args...) [cv-qualifiers] [ref-qualifiers]. So you can extend your class to deduce the first type thusly:

    template <class F> struct ArgType;
    
    template <typename Ret, typename Cls, typename T, typename... Args>
    struct ArgType<Ret (Cls::*)(T, Args...)> {
        using type = T;
    };
    

    Note that you can make this more generic by write a metafunction that gives you the nth argument:

    template <typename Ret, typename Cls, typename... Args>
    struct ArgType<Ret (Cls::*)(Args...)> {
        
        template <size_t N>
        struct get_arg {
            using type = typename std::tuple_element<N,
                            std::tuple<Args...>>::type;
        };
    };
    

    So ArgType<F>::arg<0>::type would be the type you seek.