jeudi 20 octobre 2016

Infer the first argument of a callable

I'd like to be able to infer the first argument of a callable. I can make it work for free and member functions, but I'm struggling with lambdas. Is there some trick I can use?

Here's an example. Within the match functions below, I want to use the knowledge of T.

template<class T>
void match(void (*)(T*, int))   { /* First */ }

template<class T>
void match(void (T::*)(int))    { /* Second */ }

template<class T>
void match(std::function<void(T,int)>)    { /* Third */ }

struct A
{
   void f(int)  {}
};

void g(A*, int) {}

match(&A::f);           // Ok, matches first
match(&g);              // Ok, matches second
match([](A*, int) {});  // Not Ok
match([&](A*, int) {}); // Not Ok

Aucun commentaire:

Enregistrer un commentaire