C ++ infer parameters of a member function
I want 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 removing the reference to the function pointer so that the compiler can infer the template arguments?
Would be grateful for the entrance!
thank
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;
}
source to share
Pointer members look like Ret (Cls::*)(Args...) [cv-qualifiers] [ref-qualifiers]
. This way you can extend your class to infer the first type:
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 general by writing a metafung that gives you the n
th 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 that ArgType<F>::arg<0>::type
will be the type you are looking for.
source to share