boost:bind的函数类型不对 #583
Replies: 6 comments 1 reply
-
This is not a bug. It's a feature of
[1] https://www.boost.org/doc/libs/1_73_0/libs/bind/doc/html/bind.html |
Beta Was this translation helpful? Give feedback.
-
Thank you very much. But i still don't understand, and this is a test.mybind.c
$ g++ mybind.c -o bind |
Beta Was this translation helpful? Give feedback.
-
Sorry, i have a mistake. Should be like this.
|
Beta Was this translation helpful? Give feedback.
-
What's the question here? The code works just fine: https://ideone.com/Mhjl02 #include <stdio.h>
#include <functional>
using Callback = std::function<void(int)>;
void print() {
printf("Hello\n");
}
int main() {
Callback f = std::bind(print);
f(43); // prints "Hello"
return 0;
} |
Beta Was this translation helpful? Give feedback.
-
Now, I understand that is not a bug. Can you tell me why it works well? Thanks. "f = boost::bind(myprint); // ok. why ?"
|
Beta Was this translation helpful? Give feedback.
-
Channel.h
class Channel : boost::noncopyable
{
public:
typedef boost::function<void()> EventCallback;
typedef boost::function<void(Timestamp)> ReadEventCallback;
Channel(EventLoop* loop, int fd);
~Channel();
void handleEvent(Timestamp receiveTime);
void setReadCallback(const ReadEventCallback& cb)
{ readCallback = cb; }
void setWriteCallback(const EventCallback& cb)
{ writeCallback_ = cb; }
void setCloseCallback(const EventCallback& cb)
{ closeCallback_ = cb; }
void setErrorCallback(const EventCallback& cb)
{ errorCallback_ = cb; }
EventLoop.cc
EventLoop::EventLoop()
: looping_(false),
quit_(false),
eventHandling_(false),
callingPendingFunctors_(false),
iteration_(0),
threadId_(CurrentThread::tid()),
poller_(Poller::newDefaultPoller(this)),
timerQueue_(new TimerQueue(this)),
wakeupFd_(createEventfd()),
wakeupChannel_(new Channel(this, wakeupFd_)),
currentActiveChannel_(NULL)
{
LOG_TRACE << "EventLoop created " << this << " in thread " << threadId_;
if (t_loopInThisThread)
{
LOG_FATAL << "Another EventLoop " << t_loopInThisThread
<< " exists in this thread " << threadId_;
}
else
{
t_loopInThisThread = this;
}
wakeupChannel->setReadCallback(
boost::bind(&EventLoop::handleRead, this));
// we are always reading the wakeupfd
wakeupChannel_->enableReading();
}
EventLoop::handleRead函数是没有参数的,而ReadEventCallback是带Timestamp参数的,这二者不匹配。
Beta Was this translation helpful? Give feedback.
All reactions