Boost 1.66中basic_socket_acceptor接口变更是否为错误?
basic_socket_acceptor between Boost 1.65 and 1.66 a bug? No, this is not a bug—it’s an intentional API evolution in Boost.Asio to align with modern C++ asynchronous programming patterns and the Executor model. Let’s break down what changed and why:
The parameter shift explained:
- In Boost 1.65,
basic_socket_acceptorhad three template parameters:Protocol,Executor(defaulting toio_context::executor_type), andAcceptHandler(defaulting tovoid). - In Boost 1.66, the
AcceptHandlertemplate parameter was removed entirely. Instead of binding the handler type to the acceptor class itself, asynchronous accept handlers are now passed directly as arguments toasync_accept()calls.
- In Boost 1.65,
Why this change was made:
This adjustment was part of Boost.Asio’s ongoing effort to align with the C++ standard library’s Executor design. By decoupling the handler type from the acceptor class, the API becomes:- More flexible: A single
basic_socket_acceptorinstance can work with multiple different handler types for asynchronous accepts. - More consistent: It matches the pattern used by other Boost.Asio socket types, which had already moved away from handler-specific template parameters.
- Cleaner: Reduces unnecessary template parameter bloat in the class definition.
- More flexible: A single
Compatibility note:
If your code was written for Boost 1.65 and explicitly specified theAcceptHandlertemplate parameter, you’ll need to adjust it when upgrading to 1.66. Simply remove theAcceptHandlerargument from thebasic_socket_acceptorinstantiation, and pass your handler directly toasync_accept()as you would with other asynchronous operations.
内容的提问来源于stack exchange,提问作者The Quantum Physicist

