我正在尝试在项目中使用 Asio 的非 Boost 版本。我正在编写对 stream_protocol::acceptor::async_accept
的回调。签名需要传递 asio::placeholders::error
但当我这样做时,出现以下错误:
错误:命名空间“asio::placeholders”中没有名为“error”的成员
根据源代码,我可以看到错误存在,但类型为 undefined
,这对我来说是新的。我错过了什么吗?我应该对库进行某种预处理吗?
最佳答案
简而言之,使用 std::placeholders::_1
而不是 asio::placeholders:error
。
Asio 仅在使用 Boost.Bind 时支持方便的占位符变量。 error
占位符 documentation状态:
An argument placeholder, for use with
boost::bind()
, ...
当使用 std::bind()
创建处理程序时,需要使用 std::bind
的占位符。 async_accept()
操作接受满足 AcceptHandler 的处理程序类型要求:
An accept handler must meet the requirements for a handler. A value
h
of an accept handler class should work correctly in the expressionh(ec)
, whereec
is an lvalue of typeconst error_code
.
当使用 std::bind()
创建仿函数作为 AcceptHandler 时,如果希望获得 error_code
参数,则使用 std: :placeholders::_1
:
void handle_accept(const std::error_code&);
acceptor.async_accept(server_socket, std::bind(&handle_accept,
std::placeholders::_1 /* error_code */));
这是一个完整的最小示例 demonstrating使用 std::bind()
。请注意,coliru 似乎没有可用的 Asio 独立版本,但示例应该足够了:
#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
可选地,如果希望更冗长一点,则可以使用命名占位符:
namespace asio_placeholders
{
auto error = std::placeholders::_1;
}
// ...
acceptor.async_accept(server_socket, std::bind(&handle_accept,
asio_placeholders::error));
源代码中观察到的未指定
类型仅在生成文档时使用,如本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
关于c++ - 不能在非 Boost 版本的 Asio 中使用 asio::placeholders::error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28486347/