C++ 函数deadline_timer::async_wait()的参数
我正在学习Boost.Asio,但我有一个关于Boost::Asio::deadline\u timer async\u wai的问题。以下是boost主页上的代码:C++ 函数deadline_timer::async_wait()的参数,c++,boost,C++,Boost,我正在学习Boost.Asio,但我有一个关于Boost::Asio::deadline\u timer async\u wai的问题。以下是boost主页上的代码: // // timer.cpp // ~~~~~~~~~ // // Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // Distributed under the Boost Software License, Ver
//
// timer.cpp
// ~~~~~~~~~
//
// Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer
{
public:
printer(boost::asio::io_service& io)
: timer_(io, boost::posix_time::seconds(1)),
count_(0)
{
timer_.async_wait(boost::bind(&printer::print, this));
}
~printer()
{
std::cout << "Final count is " << count_ << "\n";
}
void print()
{
if (count_ < 5)
{
std::cout << count_ << "\n";
++count_;
timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
timer_.async_wait(boost::bind(&printer::print, this));
}
}
private:
boost::asio::deadline_timer timer_;
int count_;
};
int main()
{
boost::asio::io_service io;
printer p(io);
io.run();
return 0;
}
但在这个穹顶中,它是timer\uu.async\uwait(boost::bind(&printer::print,this))代码>,签名是无效打印(打印机*)
,如何工作?请帮助我,谢谢。Boost.Bind忽略额外的参数,请查看文档 编辑: 已经有人问了一个与您类似的问题,请看一看,有更详细的答案。文本来自 在本例中,boost::bind()的boost::asio::placeholders::error参数是传递给处理程序的错误对象的命名占位符。在启动异步操作时,如果使用boost::bind(),则,您必须仅指定与处理程序的参数列表匹配的参数。在教程Timer.4中,您将看到,如果回调处理程序不需要该参数,则可以省略此占位符。“ 您不能使用boost::asio::placeholders::error,也不能在timer4示例中使用它 示例3没有boost::asio::占位符::错误
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
void print( boost::asio::deadline_timer* t, int* count)
{
if (*count < 5)
{
std::cout << *count << "\n";
++(*count);
t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
t->async_wait(boost::bind(print, t, count));
}
}
int main()
{
boost::asio::io_service io;
int count = 0;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
t.async_wait(boost::bind(print, &t, &count));
io.run();
std::cout << "Final count is " << count << "\n";
return 0;
}
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer
{
public:
printer(boost::asio::io_service& io)
: timer_(io, boost::posix_time::seconds(1)),
count_(0)
{
timer_.async_wait(boost::bind(&printer::print, this, boost::asio::placeholders::error));
}
~printer()
{
std::cout << "Final count is " << count_ << "\n";
}
void print(const boost::system::error_code &e)
{
if (count_ < 5)
{
std::cout << count_ << "\n";
++count_;
timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
timer_.async_wait(boost::bind(&printer::print, this, boost::asio::placeholders::error ));
}
}
private:
boost::asio::deadline_timer timer_;
int count_;
};
int main()
{
boost::asio::io_service io;
printer p(io);
io.run();
return 0;
}
#包括
#包括
#包括
#包括
无效打印(boost::asio::deadline\u timer*t,int*count)
{
如果(*计数小于5)
{
std::cout expires_at()+boost::posix_time::seconds(1));
t->async_wait(boost::bind(print,t,count));
}
}
int main()
{
boost::asio::io_服务io;
整数计数=0;
boost::asio::deadline_timer t(io,boost::posix_time::seconds(1));
t、 异步等待(boost::bind(打印、t和计数));
io.run();
std::cout我会使用std::bind
,尽量少用boost
东西(我在我的一个项目中使用boost.asio
,只包括boost/asio.hpp
),看看如何使用std::bind
,而不是boost::bind
。更好地理解绑定是如何工作的。@IgorR。很棒的文章!
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer
{
public:
printer(boost::asio::io_service& io)
: timer_(io, boost::posix_time::seconds(1)),
count_(0)
{
timer_.async_wait(boost::bind(&printer::print, this, boost::asio::placeholders::error));
}
~printer()
{
std::cout << "Final count is " << count_ << "\n";
}
void print(const boost::system::error_code &e)
{
if (count_ < 5)
{
std::cout << count_ << "\n";
++count_;
timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
timer_.async_wait(boost::bind(&printer::print, this, boost::asio::placeholders::error ));
}
}
private:
boost::asio::deadline_timer timer_;
int count_;
};
int main()
{
boost::asio::io_service io;
printer p(io);
io.run();
return 0;
}