问题描述
我正在尝试在项目中使用非Boost版本的Asio.我正在写一个stream_protocol::acceptor::async_accept
的回调.签名要求传递asio::placeholders::error
,但是当我这样做时,出现以下错误:
I'm trying to use the non Boost version of Asio in a project. I'm writing a callback to stream_protocol::acceptor::async_accept
. The signature requires asio::placeholders::error
to be passed but when I do so, I get the following error:
error: no member named 'error' in namespace 'asio::placeholders'
从源头开始,我可以看到错误存在,但类型为undefined
,这对我来说是新的.我想念什么吗?我是否应该对库进行某种预处理?
Following the source, I can see error is there but of type undefined
, which is new to me. Am I missing something? Am I supposed to do some sort of pre-processing of the libraries?
推荐答案
简而言之,请使用std::placeholders::_1
而不是asio::placeholders:error
.
In short, use std::placeholders::_1
instead of asio::placeholders:error
.
在使用Boost.Bind时,Asio仅支持方便的占位符变量. error
占位符文档指出:
Asio only supports the convenient placeholder variables when using Boost.Bind. The error
placeholder documentation states:
使用std::bind()
创建处理程序时,需要使用std::bind
的占位符. async_accept()
操作接受处理程序符合 AcceptHandler 类型要求:
When using std::bind()
to create handlers, one needs to use std::bind
's placeholders. The async_accept()
operation accepts a handler that meets the AcceptHandler type requirements:
使用std::bind()
创建函子以用作AcceptHandler时,如果希望获得error_code
参数,则使用std::placeholders::_1
:
When creating a functor with std::bind()
to function as a AcceptHandler, if one wishes to obtain the error_code
argument, then use std::placeholders::_1
:
void handle_accept(const std::error_code&);
acceptor.async_accept(server_socket, std::bind(&handle_accept,
std::placeholders::_1 /* error_code */));
以下是使用std::bind()
进行演示的完整的最小示例演示.请注意,cooliru似乎没有可用的Asio独立版本,但该示例应足够:
Here is a complete minimal example demonstrating using std::bind()
. Note that coliru does not appear to have an Asio standalone version available, but the example should suffice:
#include <iostream>
#include <functional>
#include <boost/asio.hpp>
void handle_accept(const boost::system::error_code& error_code)
{
std::cout << "handle_accept: " << error_code.message() << std::endl;
}
void noop() {}
int main()
{
using boost::asio::ip::tcp;
boost::asio::io_service io_service;
// Create all I/O objects.
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 0));
tcp::socket server_socket(io_service);
tcp::socket client_socket(io_service);
// Connect client and server sockets.
acceptor.async_accept(server_socket, std::bind(&handle_accept,
std::placeholders::_1 /* error_code */));
client_socket.async_connect(acceptor.local_endpoint(), std::bind(&noop));
io_service.run();
}
输出:
handle_accept: Success
可选地,如果希望更多的详细程度,则可以使用命名的占位符:
Optionally, if one wishes for a bit more verbosity, then named placeholders could be used:
namespace asio_placeholders
{
auto error = std::placeholders::_1;
}
// ...
acceptor.async_accept(server_socket, std::bind(&handle_accept,
asio_placeholders::error));
仅在生成文档时使用源代码中观察到的unspecified
类型,如此:
The unspecified
type observed in the source code is only used when generating documentation, as shown in this code:
#if defined(GENERATING_DOCUMENTATION)
/// An argument placeholder, for use with boost::bind(), that corresponds to
/// the error argument of a handler for any of the asynchronous functions.
unspecified error;
// ...
#elseif
这篇关于在非Boost版本的Asio中不能使用asio :: placeholders :: error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!