C++ lamba 表达式不支持可变参数 args...
centos
gcc 4.8.5
下编译
// 支持类成员函数
template <typename Function, typename Self, typename... Args>
void add_task(const Function &func, Self *self, Args... args)
{
if (!_is_stop_threadpool)
{
//下面这句编译失败
//task_t task = [&func, &self, args...] { return (*self.*func)(args...); };
//decltype(iarr)
//换成这样的写法也不对,那如何获取到类名呢
using type = decltype (self);
std::function<void ()> task = std::bind(&type::func, self, args...);
add_task_impl(task);
}
}
class Test
{
public:
void print(const std::string &str, int i)
{
std::cout << "Test: " << str << ", i: " << i << std::endl;
}
};
//调用测试
Test t;
add_task(&Test::print, &t,str, i);
错误:‘func’不是‘type {aka Test*}’的成员
std::function<void ()> task = std::bind(&type::func, self, args...);