Perfect Forwarding Variadic Template to Standard Thread

强颜欢笑 提交于 2019-12-05 09:54:19

This is one of those rare cases where the difference between passing a function and passing a function pointer makes a difference. If you do:

pool.Binder(&Simple2, 3, 4);  

it should work. Or alternatively you can have Binder decay its argument to a function pointer:

class Pool {
 public:
  template <typename Function, typename... Args>
  void Binder(Function&& f, Args&&... a) {
    std::thread t(Wrapper<typename std::decay<Function>::type, Args...>,
                  std::forward<Function>(f), std::forward<Args>(a)...);
  }
};

which in C++14 simplifies to:

class Pool {
 public:
  template <typename Function, typename... Args>
  void Binder(Function&& f, Args&&... a) {
    std::thread t(Wrapper<std::decay_t<Function>, Args...>,
                  std::forward<Function>(f), std::forward<Args>(a)...);
  }
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!