vendredi 9 janvier 2015

Get function arity from template parameter

How can I get the arity of an arbitrary function type used as a template parameter?


The function can be a normal function, a lambda or a functor. Example:



template<typename TFunc>
std::size_t getArity()
{
// ...?
}

template<typename TFunc>
void printArity(TFunc mFunc)
{
std::cout << "arity: " << getArity<TFunc>() << std::endl;
}

void testFunc(int) { }

int main()
{
printArity([](){}); // prints 0
printArity([&](int x, float y){}); // prints 2
printArity(testFunc); // prints 1
}


I have access to all C++14 features.


Do I have to create specialization for every function type (and all respective qualifiers)? Or is there an easier way?


Aucun commentaire:

Enregistrer un commentaire