我希望将所有函数(以元组作为参数)传递给Foo,将其转换为采用简单参数的函数。
auto f = [](const std::tuple<int, char>& t) { return std::get<0>(t); };
template <typename F>
auto Foo(F&& f) {
    ...
}
Foo(f)(42, 'a')我的想法是做这样的事
template <typename ReturnType, typename... Args>
auto Foo(std::function<ReturnType(const std::tuple<Args...>&)> f) {
    return [&](Args&&... args) -> ReturnType {
      return f({std::forward<Args>(args)...});  
    };
}但是编译器不能为lambda推断模板参数,因此忽略了这个候选参数。
发布于 2021-09-19 09:23:11
如果你真的不需要参数类型。简单地说
template <typename Callable>
auto Foo(Callable f) {
    return [=](auto&&... args)->decltype(auto){
      return f({std::forward<decltype(args)>(args)...});
    };
}好的一面是Foo现在可以接受更通用的f,它可以定义多个opreator()。
https://stackoverflow.com/questions/69241697
复制相似问题