小编Yan*_*eng的帖子

为什么模板不丢弃 co_return?

我想创建一个具有同步和协程版本的函数,而不使用模板专门化,即使用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 类型实现的完整代码

c++ c++20 c++-coroutine c++-templates

8
推荐指数
1
解决办法
280
查看次数

标签 统计

c++ ×1

c++-coroutine ×1

c++-templates ×1

c++20 ×1