函数deadline_timer::async_wait()的参数
The argument of function deadline_timer::async_wait()
我正在学习Boost.Asio,但我有一个关于Boost::Asio::deadline_timer async_wai的问题。这是来自boost主页的代码:
//
// 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;
}
async_wait需要这样的函数签名:
void handler(
const boost::system::error_code& error // Result of operation.
);
但在这个圆顶中,它是timer_.async_wait(boost::bind(&printer::print, this));
,签名是void print(printer*)
,它是如何工作的
请帮帮我,谢谢。
timer3示例中的文本"在本例中,boost::bind()的boost:,必须仅指定与处理程序的参数列表匹配的参数。在Timer.4教程中,您将看到,如果回调处理程序不需要参数,则可能会忽略此占位符。"
您不能使用boost::asio::占位符::error或在timer4示例中使用它。
示例3没有boost::asio::占位符::error
#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;
}
示例4带有boost::asio::占位符::error
#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.Bind会忽略额外的参数,请查看Bind documentaion。
编辑:
已经有人问了一个与你类似的问题,看看吧,有更详细的答案。
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 使用用户定义的参数调用future/async并调用类方法
- 为什么我不能将引用作为 std::async 的函数参数传递
- 如何将函数及其参数传递给成员函数内的 std::async
- 为什么不能将std :: async用于接收对抽象类作为参数的函数
- 没有与参数列表匹配的重载函数"async"实例
- 在std::async或std::bind中传递参数
- 如何将函数参数传递给boost::async()
- std::async, std::函数对象和带有'callable'参数的模板
- std::async - 参数向量已损坏
- 为什么 std::async 会复制它的 const 和参数?
- 关于由 std::async 与 std::launch::async 参数启动的线程的混淆