【发布时间】:2015-10-27 15:21:38
【问题描述】:
我想转接这个电话:
timer.async_wait(handler);
进入这个电话:
func(handler);
所以我尝试使用std::bind:
#include <functional>
#include <boost/asio.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/system/error_code.hpp>
using std::bind;
using std::function;
using boost::asio::io_service;
using boost::asio::steady_timer;
using boost::system::error_code;
int main(int, char**) {
using namespace std::placeholders;
io_service io_s;
steady_timer timer (io_s);
// error
function<void(function<void(const error_code&)>)> bound =
bind(&steady_timer::async_wait, timer, _1);
return 0;
}
但它不会编译!像这样编译时:
g++-4.9 -std=c++11 -Wall -I/usr/local/include -L/usr/local/lib -lboost_system -o bin main.cpp
错误信息是这样的:
main.cpp:22:46: error: no matching function for call to 'bind(<unresolved overloaded function type>, boost::asio::steady_timer&, const std::_Placeholder<1>&)'
bind(&steady_timer::async_wait, timer, _1);
我尝试了各种方法(如强制转换、模板参数、函数对象),但无法编译。
我正在使用 Boost ASIO 1.58,the documentation 表示以下内容:
template<
typename WaitHandler>
void-or-deduced async_wait(
WaitHandler handler);
here 你可以看到 WaitHandler 是什么。我不明白void-or-deduced 是什么意思,但我想这可能是什么让我很难过?我已经盯着这个问题看了很长时间,以至于我认为我已经获得了隧道视野。
在这种情况下如何正确使用std::bind?
【问题讨论】:
标签: c++ c++11 boost boost-asio