我想创建一个具有同步和协程版本的函数,而不使用模板专门化,即使用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)
为什么这不起作用?