Skip to content

Conversation

@spoonincode
Copy link
Contributor

No description provided.

auto task = std::make_shared<std::packaged_task<decltype( f() )()>>( std::forward<F>( f ) );
boost::asio::post( ioc, [task]() { (*task)(); } );
return task->get_future();
std::future<std::invoke_result_t<std::decay_t<F>>> post_async_task( boost::asio::io_context& ioc, F&& f ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you specify the return type of post_async_task for clarity (as opposed to auto)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did. I don't mind changing back if desired.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a little verbose, but at least it makes the return value crystal clear. I say keep as is.

You could also use:

return boost::asio::post( ioc, std::packaged_task(std::forward<F>(f)) );

which seems a bit clearer to me but again either way is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use_future is more idiomatic. It's well documented and used in many of asio's examples. Whereas being able to pass a packaged_task is only mentioned deep in the reference docs and never used in examples.

@spoonincode spoonincode merged commit 86fdc05 into main Apr 14, 2025
36 checks passed
@spoonincode spoonincode deleted the rm_packaged_task branch April 14, 2025 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants