How is it possible to access the types of the parameters of a lambda function in c++? The following does not work:
template <class T> struct capture_lambda {
};
template <class R, class T> struct capture_lambda<R(T)> {
static void exec() {
}
};
template <class T> void test(T t) {
capture_lambda<T>::exec();
}
int main() {
test([](int i)->int{ return 0; });
}
The above does not compile, because the compiler chooses the template prototype instead of the specialization.
Is there a way to do the above?
What I am actually trying to achieve is this: I have a list of functions and I want to select the appropriate function to invoke. Example:
template <class T, class ...F> void exec(T t, F... f...) {
//select the appropriate function from 'F' to invoke, based on match with T.
}
For example, I want to invoke the function that takes 'int':
exec(1, [](char c){ printf("Error"); }, [](int i){ printf("Ok"); });
This isn't possible, lambda functions are syntactic sugar for creating function objects not actual functions. This means that the template is accepting a class, and classes don't have the concept of argument type.
Also keep in mind that a general function object can have any number of overloaded operator()
s.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With