Using std::function
, we can get the type of an argument using the argument_type
, second_argument_type
etc. typedefs, but I can\'t see
@Luc's answer is great but I just came across a case where I also needed to deal with function pointers:
template
Arg first_argument_helper(Ret(*) (Arg, Rest...));
template
Arg first_argument_helper(Ret(F::*) (Arg, Rest...));
template
Arg first_argument_helper(Ret(F::*) (Arg, Rest...) const);
template
decltype(first_argument_helper(&F::operator())) first_argument_helper(F);
template
using first_argument = decltype(first_argument_helper(std::declval()));
This can be used on both functors and function pointers:
void function(float);
struct functor {
void operator() (int);
};
int main() {
std::cout << std::is_same, int>::value
<< ", "
<< std::is_same, int>::value
<< std::endl;
return 0;
}