vendredi 13 mars 2015

Deduce function object argument types


Given a set of argument types and a function object, the return type can be deduced with decltype. What about deducing argument types?


For a function pointer, both return and argument types can be deduced with a funny pattern matching syntax. As an example, here's a silly program that uses deduced argument types to print out a string describing the return and argument types of a function pointer.



#include <iostream>
#include <string>

using std::string;

template<class T>
string type_str();

template<>
string type_str<int>() { return "int"; }

template<>
string type_str<char>() { return "char"; }

string arg_types_str()
{
return "";
}

template<class T>
string arg_types_str()
{
return type_str<T>();
}

template<class T, class U, class... Args>
string arg_types_str()
{
return type_str<T>() + ", " + arg_types_str<U, Args...>();
}

template<class R, class... Args>
void print_fptr_type(R (*fptr)(Args...))
{
std::cout << type_str<R>() << " (*)(" << arg_types_str<Args...>() << ")" << std::endl;
}

int main()
{
int (*fptr)(char, int);
print_fptr_type(fptr);
}


Output:



int (*)(char, int)


Live demo.


Is it possible to write a program similar to the example that instead prints the return and argument types of function objects?


It seems like for a function object with exactly one operator(), the argument types can in principle be deduced unambiguously.




Aucun commentaire:

Enregistrer un commentaire