From 599104c16a3b4503be2597bd797f7772a9043592 Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Sat, 2 Feb 2019 10:20:38 -0800 Subject: [PATCH] async_echo supports move-only handlers fix #1420 --- CHANGELOG.md | 1 + example/echo-op/echo_op.cpp | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 748fe1cf..cd90f994 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ Version 209: * Faster http::string_to_field +* async_echo supports move-only handlers -------------------------------------------------------------------------------- diff --git a/example/echo-op/echo_op.cpp b/example/echo-op/echo_op.cpp index 363df50c..bbe12c16 100644 --- a/example/echo-op/echo_op.cpp +++ b/example/echo-op/echo_op.cpp @@ -277,7 +277,7 @@ async_echo(AsyncStream& stream, CompletionToken&& token) BOOST_ASIO_HANDLER_TYPE( CompletionToken, void(boost::beast::error_code))>{ stream, - init.completion_handler}(boost::beast::error_code{}, 0); + std::move(init.completion_handler)}({}, 0); // This hook lets the caller see a return value when appropriate. // For example this might return std::future if @@ -289,6 +289,18 @@ async_echo(AsyncStream& stream, CompletionToken&& token) //] +struct move_only_handler +{ + move_only_handler(move_only_handler&&) = default; + move_only_handler(move_only_handler const&) = delete; + + void operator()(boost::beast::error_code ec) + { + if(ec) + std::cerr << ": " << ec.message() << std::endl; + } +}; + int main(int argc, char** argv) { if(argc != 3) @@ -317,12 +329,7 @@ int main(int argc, char** argv) acceptor.bind(ep); acceptor.listen(); acceptor.accept(sock); - async_echo(sock, - [&](boost::beast::error_code ec) - { - if(ec) - std::cerr << argv[0] << ": " << ec.message() << std::endl; - }); + async_echo(sock, move_only_handler{}); ioc.run(); return EXIT_SUCCESS; }