瑞星 发表于 2023-10-4 13:01:27

函数模板_构造函数栈溢出

前言

最近写一个任务队列,可以支持存入返回值为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_中。
支持形如
auto f1 = [](int i, int j){    std::cout
页: [1]
查看完整版本: 函数模板_构造函数栈溢出