使用参数包在C ++中编写lambda函数

问题描述 投票:0回答:1

我是C ++ 14的新手,我想将可变长度的lambda函数组成一个单一的lambda,我应该怎么做?以下是我目前的工作

#include <iostream>

template<typename Ftype>
Ftype compose(const Ftype & fn) {
  return fn;
}

template<typename Ftype, typename... Other>
auto compose(Ftype fn, Other... other) {
  return [=](auto x){return other(fn(x))...;};
}        ➤ expression contains unexpanded parameter pack 'other'

int main(void) {
  auto add_func = [](const int x) { return x * 7; };
  auto sub_func = [](const int x) { return x + 1; };
  int res = compose(add_func, sub_func)(1);
  std::cout << "Result: " << res << "\n";
}

但是我编译失败,我想我可能以某种方式使用了lambda或variadic参数。有人可以帮我吗?

c++ lambda c++14 variadic-templates compose
1个回答
0
投票

您的“递归”案例不包含对compose的调用,这应该是您在哪里混淆了;)]]

return [=](auto x){ return compose(other...)(fn(x)); };
© www.soinside.com 2019 - 2024. All rights reserved.