如何在 c++ 中访问 lambda 函数的参数类型?以下不起作用:
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; });
}
上面没有编译,因为编译器选择了模板原型而不是特化。
有没有办法做到以上几点?
我实际上想要实现的是:我有一个函数列表,我想选择适当的函数来调用。例子:
template <class T, class ...F> void exec(T t, F... f...) {
//select the appropriate function from 'F' to invoke, based on match with T.
}
例如,我想调用采用'int'的函数:
exec(1, [](char c){ printf("Error"); }, [](int i){ printf("Ok"); });