You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用C++23运行Boost.Asio异步Daytime服务器示例遇绑定与类型错误

Boost.Asio 1.87异步Daytime服务器C++23编译错误排查与解决

错误1:async_write调用触发concept约束不满足

编译错误信息:

main.cpp:47:9: No matching function for call to 'async_write'
write.hpp:: candidate template ignored: constraints not satisfied [with AsyncWriteStream = tcp::socket, ConstBufferSequence = mutable_buffers_1, WriteToken = typename _Bind_helper<__is_socketlike<void (tcp_connection::*)(const error_code &, unsigned long)>::value, void (tcp_connection::*)(const error_code &, unsigned long), shared_ptr<tcp_connection>, arg<1> (&)(), arg<2> (&)()>::type]
:564:5: note: because '::boost::asio::completion_token_for<std::_Bind<void (tcp_connection::*(std::shared_ptr<tcp_connection>, boost::arg<1> (*)(), boost::arg<2> (*)()))(const boost::system::error_code &, unsigned long)>, void (boost::system::error_code, std::size_t)>' evaluated to false
async_result.hpp:1214:5: because 'async_initiate<T, Signatures...>(detail::initiation_archetype<Signatures...>{}, t)' would be invalid: no matching function for call to 'async_initiate'
write.hpp:: candidate template ignored: substitution failure [with AsyncWriteStream = tcp::socket, DynamicBuffer_v1 = mutable_buffers_1, WriteToken = typename _Bind_helper<__is_socketlike<void (tcp_connection::*)(const error_code &, unsigned long)>::value, void (tcp_connection::*)(const error_code &, unsigned long), shared_ptr<tcp_connection>, arg<1> (&)(), arg<2> (&)()>::type]: no type named 'type' in 'boost::asio::constraint<false>'
write.hpp:: candidate template ignored: could not match 'boost::asio::basic_streambuf<Allocator>' against 'mutable_buffers_1'
write.hpp:: candidate template ignored: substitution failure [with AsyncWriteStream = tcp::socket, DynamicBuffer_v2 = mutable_buffers_1, WriteToken = typename _Bind_helper<__is_socketlike<void (tcp_connection::*)(const error_code &, unsigned long)>::value, void (tcp_connection::*)(const error_code &, unsigned long), shared_ptr<tcp_connection>, arg<1> (&)(), arg<2> (&)()>::type]: no type named 'type' in 'boost::asio::constraint<false>'
write.hpp:: candidate function template not viable: requires 4 arguments, but 3 were provided
write.hpp:: candidate function template not viable: requires at least 4 arguments, but 3 were provided
write.hpp:: candidate function template not viable: requires at least 4 arguments, but 3 were provided
write.hpp:: candidate function template not viable: requires at least 4 arguments, but 3 were provided

原因

C++23对concept的严格检查下,std::bind生成的绑定对象无法满足Boost.Asio的completion_token_for概念,Asio异步操作要求完成令牌精准匹配回调签名,而std::bind的类型推导逻辑与Asio的检查机制不兼容。

解决方案

用lambda表达式替代std::bind,lambda能天然适配Asio的完成令牌要求,同时代码可读性更强:

原错误代码:

async_write(socket_, boost::asio::buffer(message_),
    std::bind(&tcp_connection::handle_write, shared_from_this(),
      boost::asio::placeholders::error,
      boost::asio::placeholders::bytes_transferred));

修改后代码:

async_write(socket_, boost::asio::buffer(message_),
    [self = shared_from_this()](const boost::system::error_code& ec, std::size_t bytes_transferred) {
        self->handle_write(ec, bytes_transferred);
    });

错误2:async_accept调用出现类型转换错误

编译错误信息:

main.cpp:84:9: No matching member function for call to 'async_accept'
basic_socket_acceptor.hpp:1531:3: candidate function template not viable: no known conversion from 'typename _Bind_helper<__is_socketlike<void (tcp_server::*)(shared_ptr<tcp_connection>, const error_code &)>::value, void (tcp_server::*)(shared_ptr<tcp_connection>, const error_code &), tcp_server *, shared_ptr<tcp_connection> &, arg<1> (&)()>::type' (aka '_Bind<void (tcp_server::*(tcp_server *, std::shared_ptr<tcp_connection>, boost::arg<1> (*)()))(std::shared_ptr<tcp_connection>, const boost::system::error_code &)>') to 'endpoint_type &' (aka 'basic_endpoint<tcp> &') for 2nd argument
basic_socket_acceptor.hpp:2500:3: candidate function template not viable: no known conversion from 'typename _Bind_helper<__is_socketlike<void (tcp_server::*)(shared_ptr<tcp_connection>, const error_code &)>::value, void (tcp_server::*)(shared_ptr<tcp_connection>, const error_code &), tcp_server *, shared_ptr<tcp_connection> &, arg<1> (&)()>::type' (aka '_Bind<void (tcp_server::*(tcp_server *, std::shared_ptr<tcp_connection>, boost::arg<1> (*)()))(std::shared_ptr<tcp_connection>, const boost::system::error_code &)>') to 'endpoint_type &' (aka 'basic_endpoint<tcp> &') for 2nd argument
basic_socket_acceptor.hpp:2603:3: candidate function template not viable: no known conversion from 'typename _Bind_helper<__is_socketlike<void (tcp_server::*)(shared_ptr<tcp_connection>, const error_code &)>::value, void (tcp_server::*)(shared_ptr<tcp_connection>, const error_code &), tcp_server *, shared_ptr<tcp_connection> &, arg<1> (&)()>::type' (aka '_Bind<void (tcp_server::*(tcp_server *, std::shared_ptr<tcp_connection>, boost::arg<1> (*)()))(std::shared_ptr<tcp_connection>, const boost::system::error_code &)>') to 'endpoint_type &' (aka 'basic_endpoint<tcp> &') for 2nd argument
basic_socket_acceptor.hpp:2211:3: candidate function template not viable: no known conversion from 'tcp::socket' (aka 'basic_stream_socket<tcp>') to 'endpoint_type &' (aka 'basic_endpoint<tcp> &') for 1st argument
basic_socket_acceptor.hpp:1398:3: candidate template ignored: constraints not satisfied [with Protocol1 = boost::asio::ip::tcp, Executor1 = boost::asio::any_io_executor, AcceptToken = typename _Bind_helper<__is_socketlike<void (tcp_server::*)(shared_ptr<tcp_connection>, const error_code &)>::value, void (tcp_server::*)(shared_ptr<tcp_connection>, const error_code &), tcp_server *, shared_ptr<tcp_connection> &, arg<1> (&)()>::type]
:1394:7: note: because '::boost::asio::completion_token_for<std::_Bind<void (tcp_server::*(tcp_server *, std::shared_ptr<tcp_connection>, boost::arg<1> (*)()))(std::shared_ptr<tcp_connection>, const boost::system::error_code &)>, void (boost::system::error_code)>' evaluated to false
async_result.hpp:1214:5: because 'async_initiate<T, Signatures...>(detail::initiation_archetype<Signatures...>{}, t)' would be invalid: no matching function for call to 'async_initiate'
basic_socket_acceptor.hpp:1936:3: candidate template ignored: substitution failure [with Executor1 = tcp::socket, MoveAcceptToken = typename _Bind_helper<__is_socketlike<void (tcp_server::*)(shared_ptr<tcp_connection>, const error_code &)>::value, void (tcp_server::*)(shared_ptr<tcp_connection>, const error_code &), tcp_server *, shared_ptr<tcp_connection> &, arg<1> (&)()>::type]: no type named 'type' in 'boost::asio::constraint<false>'
basic_socket_acceptor.hpp:2035:3: candidate template ignored: substitution failure [with ExecutionContext = tcp::socket, MoveAcceptToken = typename _Bind_helper<__is_socketlike<void (tcp_server::*)(shared_ptr<tcp_connection>, const error_code &)>::value, void (tcp_server::*)(shared_ptr<tcp_connection>, const error_code &), tcp_server *, shared_ptr<tcp_connection> &, arg<1> (&)()>::type]: no type named 'type' in 'boost::asio::constraint<false>'
basic_socket_acceptor.hpp:1679:3: candidate function template not viable: allows at most single argument 'token', but 2 arguments were provided

原因

std::bind生成的回调对象被编译器误识别为async_accept的endpoint_type参数,导致参数类型不匹配,本质还是std::bind的类型无法适配Asio的异步操作签名要求。

解决方案

同样替换std::bind为lambda表达式:

原错误代码:

acceptor_.async_accept(new_connection_->socket(),
    std::bind(&tcp_server::handle_accept, this, new_connection_,
      boost::asio::placeholders::error));

修改后代码:

acceptor_.async_accept(new_connection_->socket(),
    [this, new_connection](const boost::system::error_code& ec) {
        handle_accept(new_connection, ec);
    });

补充说明

Boost.Asio从1.70版本开始官方推荐使用lambda替代std::bind作为完成令牌,C14及以上版本的lambda支持捕获外部变量,语法更直观,同时能避免std::bind带来的类型推导和concept兼容问题,C23对concept的严格检查进一步放大了这一差异。

内容的提问来源于stack exchange,提问作者James S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 06:37:02