前言
最近写一个任务队列,可以支持存入返回值为void的任意函数对象。需要定义一个Task模板,来存储函数对象以及参数。大致的实现如下:- class Task
- {
- public:
- template <typename Func, typename... Args>
- Task(Func&& f, Args &&...args)
- : func_(std::bind(std::forward<Func>(f), std::forward<Args>(args)...)) {}
- void operator()()
- {
- func_();
- }
- private:
- std::function<void()> func_;
- };
复制代码 其中构造函数是一个函数模板,可以在编译的时候,根据传入的函数对象和参数,绑定生成std::function,存储在func_中。
支持形如
[code]auto f1 = [](int i, int j){ std::cout |