首页 > 解决方案 > Is it possible to co_await in a catch clause?

问题描述

The following code:

try {
  throw 42;
} catch (int i) {
  co_await somefuture;
}

compiles with clang 6 and 7 with -fcoroutines-ts. However, it does not compile with Visual C++ 15 (2017) Visual C++ 16 (2019) with /await with the error

C2304: 'co_await' cannot be used inside of a catch block

The C++20 standard draft and cppreference do not seem to mention anything about it.

Is it a missing feature in the Microsoft compiler or did I misunderstand the standard?

标签: c++c++20c++-coroutine

解决方案


来自[expr.await],强调我的:

await-expression应仅出现在处理程序之外的函数体的复合语句中的潜在评估表达式中([except])

MSVC 拒绝是正确的。


推荐阅读