跪求答疑, 猜测是 c++11 function 绑定使用的不得当方面的错误

是这样, 我写了一个定时器, 里面有一个回调函数 func


struct Timer 
{
    std::function<void(int)> func;   //定时函数----关闭那些不活跃的 sockfd
    //...
    Timer(int fd) : clientFd(fd)
    {
        //...
    }
};

有一个时间轮


class TimeWheel 
{
private:
    std::array<std::list<Timer*>, N> slots; //时间轮 N 个槽, Si秒转一次
    //...
 
public:
    //...
     void tick();
};

时间轮的 tick 函数

void TimeWheel::tick() 
{
    auto it = slots[currentSlot].begin();
    //...
            (*it)->func((*it)->clientFd);  //执行定时任务
            slots[currentSlot].erase(it);
 //...
}

然后再 epoll 中有一个时间轮变量 timeWheel, 每有一个新的连接就封装一个定时器你,添加到时间轮的对应槽中,每定时器到期就执行 tick

void Epoll::shutDownFd(int fd) 
{
    // int op = EPOLL_CTL_DEL;
    // epoll_Ctl(fd, op);
    close(fd);
}

void Epoll::addToTimeWheel(int fd) //将新连接 fd 封装为定时器添加到时间轮
{
    Timer* timer = new Timer(fd);
    timer->func = bind(&Epoll::shutDownFd, *this, placeholders::_1);
    timeWheel.addTimer(timer);
}


//用 timerfd_create 创建的定时器并添加到 epoll 监听
if (events[i].data.fd == timerFd) 
{
   timeWheel.tick();   //定时任务
}

但是现在 g++ 编译报错, 报错信息有好几页主要是:

 no matching function for call to ‘std::tuple<Epoll, std::_Placeholder<1> >::tuple(Epoll&, const std::_Placeholder<1>&)’
  : _M_f(std::move(__f)), _M_bound_args(std::forward<_Args>(__args)...)
             
/usr/include/c++/7/tuple:981:16: error: no type named ‘type’ in ‘struct std::enable_if<false, bool>’
  bool>::type = false>


/usr/include/c++/7/tuple:970:16: error: no type named ‘type’ in ‘struct std::enable_if<false, bool>’
  bool>::type = true>
          
                                                         ^
/usr/include/c++/7/tuple:955:16: error: no type named ‘type’ in ‘struct std::enable_if<false, bool>’
  bool>::type = false>
   

这种等等, 改查了一天还是没有什么头绪, 跪求大佬指点, 感激不尽

阅读 2.1k
1 个回答

bind函数的第二个参数应该是指针吧

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题