为什么模板不丢弃 co_return?

Yan*_*eng 8 c++ c++20 c++-coroutine c++-templates

我想创建一个具有同步和协程版本的函数,而不使用模板专门化,即使用if constexpr.

这是我写的函数:

template <Async _a>
AsyncResult<int, _a> func(int a) {
  if constexpr (_a == Async::Disable)
    return a;
  else
    co_return a;
}
Run Code Online (Sandbox Code Playgroud)

但是当我实例化真正的分支时,它给出了一个错误

auto a = func<Async::Disable>(1); // compiler error
auto b = func<Async::Enable>(2);  // ok
Run Code Online (Sandbox Code Playgroud)
error: unable to find the promise type for this coroutine
Run Code Online (Sandbox Code Playgroud)

为什么这不起作用?

包含 Promise 类型实现的完整代码

cig*_*ien 11

该标准明确指出这是不可能的。根据stmt.return.coroutine#1中的注释 1

\n
\n

...协程不应包含返回语句([stmt.return])。

\n

[注1:对于此确定,return 语句是否包含在废弃语句([stmt.if]) 中是无关紧要的。\xe2\x80\x94 尾注]

\n
\n

因此,即使协程位于废弃的语句中,您也无法从协程返回。您可以专门化函数模板而不是使用if constexpr.

\n
template <Async _a>\nAsyncResult<int, _a> func(int a) \n{\n    co_return a;\n}\n\ntemplate <>\nAsyncResult<int, Async::Disable> func<Async::Disable>(int a) \n{\n    return a;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

这是一个演示

\n