Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

co_await inside catch no longer compiling with GCC12

I have a code that looks like this:

auto func() -> asio::awaitable<void> {
    try {
        co_await async_operation();
    } catch(boost::system::system_error const& e) {
        co_return co_await another_async_operation();
    }
}

This code worked perfectly with GCC 11, but with GCC 12 it won’t compile:

file.cpp:3:19: error: await expressions are not permitted in handlers
  3 |         co_return co_await another_async_operation();
    |                   ^~~~~~~~

Why is that and how can I fix it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is explicitly forbidden in [expr.await]/2:

An await-expression shall appear only in a potentially-evaluated expression within the compound-statement of a function-body outside of a handler ([except.pre]).

The error message here is pretty clear: you can’t await in an exception handler. That it compiled before is a bug, this was always the rule (P0912R5).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading