0

How can I test weather a object is a lambda object? Is there a type_trait predicate that tests for it? It want to predicate only to return true it it is a lambda object, false if std::function, function pointer or other. Below is the test code:

#include <iostream>
#include <functional>

template <typename T,typename = typename std::enable_if_t<std::is_integral<T>::value>>
void do_stuff(T&& t) {
    std::cout << "is integral\n";
}

template <typename T /* type of predicate do I need to use to match a lambda? */ >
void do_stuff(T&& t) {
    std::cout << "is lambda\n";
}

int main(int argc, char **argv) {
    int a = 10;
    do_stuff(a);

    /* what type of predicate do I need to use above? */
    do_stuff([](){});
    return 0;
}
Konrad Eisele
  • 3,088
  • 20
  • 35
  • 4
    How do you want it to handle plain old functors like `struct foo { void operator(){} }; doo_stuff(foo{})`? – NathanOliver Jan 15 '19 at 20:58
  • You can't distinguish lambda from other functors. But, you can enable the overload for everything which is an object, has operator() and is not an `std::function` - this the best you can do. – SergeyA Jan 15 '19 at 21:07
  • @SergeyA : That would do it for me. How is this described in type trait terms? – Konrad Eisele Jan 15 '19 at 21:24
  • There is no single type trait. You'd have to conjure it yourself. This question is closed now, so I can't post an answer and the code won't fit into the comments. – SergeyA Jan 15 '19 at 21:31

0 Answers0