我想创建一个模板类或函数,它接收 lambda,并将其内部放入 std::function<> Lambda 可以有任意数量的输入参数 [](int a, float b, ...) std:: function<> 应该对应于 lambda 的 operator() 的类型
template <typename T>
void getLambda(T t) {
// typedef lambda_traits::ret_type RetType; ??
// typedef lambda_traits::param_tuple --> somehow back to parameter pack Args...
std::function<RetType(Args...)> fun(t);
}
int main() {
int x = 0;
getLambda([&x](int a, float b, Person c){});
}
Run Code Online (Sandbox Code Playgroud)
所以我需要以某种方式提取返回类型和参数包
这里的答案建议在 lambda 的 :: operator() 上使用部分规范
template <typename T>
struct function_traits : public function_traits<decltype(&T::operator())>
{};
template <typename ClassType, typename ReturnType, typename... Args> …Run Code Online (Sandbox Code Playgroud)