鍍金池/ 問答/C++/ 跪求答疑, 猜測是 c++11 function 綁定使用的不得當(dāng)方面的錯(cuò)誤

跪求答疑, 猜測是 c++11 function 綁定使用的不得當(dāng)方面的錯(cuò)誤

是這樣, 我寫了一個(gè)定時(shí)器, 里面有一個(gè)回調(diào)函數(shù) func


struct Timer 
{
    std::function<void(int)> func;   //定時(shí)函數(shù)----關(guān)閉那些不活躍的 sockfd
    //...
    Timer(int fd) : clientFd(fd)
    {
        //...
    }
};

有一個(gè)時(shí)間輪


class TimeWheel 
{
private:
    std::array<std::list<Timer*>, N> slots; //時(shí)間輪 N 個(gè)槽, Si秒轉(zhuǎn)一次
    //...
 
public:
    //...
     void tick();
};

時(shí)間輪的 tick 函數(shù)

void TimeWheel::tick() 
{
    auto it = slots[currentSlot].begin();
    //...
            (*it)->func((*it)->clientFd);  //執(zhí)行定時(shí)任務(wù)
            slots[currentSlot].erase(it);
 //...
}

然后再 epoll 中有一個(gè)時(shí)間輪變量 timeWheel, 每有一個(gè)新的連接就封裝一個(gè)定時(shí)器你,添加到時(shí)間輪的對(duì)應(yīng)槽中,每定時(shí)器到期就執(zhí)行 tick

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

void Epoll::addToTimeWheel(int fd) //將新連接 fd 封裝為定時(shí)器添加到時(shí)間輪
{
    Timer* timer = new Timer(fd);
    timer->func = bind(&Epoll::shutDownFd, *this, placeholders::_1);
    timeWheel.addTimer(timer);
}


//用 timerfd_create 創(chuàng)建的定時(shí)器并添加到 epoll 監(jiān)聽
if (events[i].data.fd == timerFd) 
{
   timeWheel.tick();   //定時(shí)任務(wù)
}

但是現(xiàn)在 g++ 編譯報(bào)錯(cuò), 報(bào)錯(cuò)信息有好幾頁主要是:

 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>
   

這種等等, 改查了一天還是沒有什么頭緒, 跪求大佬指點(diǎn), 感激不盡

回答
編輯回答
卟乖

bind函數(shù)的第二個(gè)參數(shù)應(yīng)該是指針吧

2017年6月9日 06:35