- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
#include <coroutine>
#include <string>
template<typename T>
struct Awaiter final {
bool await_ready() const { return false; }
void await_suspend(std::coroutine_handle<>) const {}
T await_resume() const { return T{}; }
};
struct ReturnObject {
struct promise_type {
ReturnObject get_return_object() { return {}; }
std::suspend_never initial_suspend()
noexcept { return {}; }
std::suspend_never final_suspend()
noexcept { return {}; }
void return_void() {}
void unhandled_exception() {}
};
};
ReturnObject f()
{
auto a1 = Awaiter<int>{};
[[maybe_unused]] auto v1 = co_await a1; // ok
auto a2 = Awaiter<std::string>{};
[[maybe_unused]] auto v2 = co_await a2; // error
}
int main() { f(); }
见:
online demo
error: no suspend point info for ''co_await' not supported
by dump_decl<declaration error>'
37 | [[maybe_unused]] auto v2 = co_await a2; // error
| ^~~~~~~~
为什么 co_await 不能返回字符串?
最佳答案
这是 GCC 中协程实现中的一个编译器错误,因为当前的标准草案中没有任何内容禁止 await_resume
的自定义/复合类型。 (用任何用户定义的类型替换字符串时可以看到)。
完全相同的代码,例如 compiles使用最新版本的 MSVC,使用 /std:c++latest
标志(这并不奇怪,因为在草案中突出开发的 Gor Nishanov 使用 Visual Studio 实现了一个原型(prototype),这可能是在撰写本答案时经过最多测试的实现)。
关于c++ - 为什么 co_await 不能返回字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67860049/
GCC 允许从 catch 恢复 C++20 协程部分并在协程中调用 co_await再次来自其 catch部分。 在这种情况下,当前处理的异常被认为是什么? 请考虑一个例子: #include #
我尝试尝试本文中提到的 co_await https://blogs.msdn.microsoft.com/vcblog/2016/04/04/using-c-coroutines-to-simpli
我在 Internet 上进行了搜索,试图找到 co_await 运算符的作用,但我仍然无法理解。我猜是下面的代码: co_await foo(); 暂停协程直到 foo 完成,但在这种情况下它与简单
我有一个异步函数 void async_foo(A& a, B& b, C&c, function callback); 我想在无堆栈协程中使用它,所以我写 auto coro_foo(A& a, B
#include #include template struct Awaiter final { bool await_ready() const { return false; }
我想知道是否可以找到可等待的 co_await 的结果类型: 由于无法在未评估的上下文中使用 co_await,我不能这样做 template task<> f(Awaitable&& awaitab
我想知道 co_waiting 具有通过引用捕获的临时值的协程是否有效。那么下面的代码是否有效,我想编译器是否应该将临时变量放在协程框架中作为局部变量: task g(const S&); task
以下代码: try { throw 42; } catch (int i) { co_await somefuture; } 使用带有 -fcoroutines-ts 的 clang 6 和
我有一个 Win32 程序,我想在其中添加一些 winRT 调用。除其他事项外,我想打开一个没有 GUI 界面的文件。 我使用 StorageFile 类的异步文件打开调用,因为下一个调用需要一个 I
我正在尝试使用 boost asio 的 C++20 协程。我目前的意图是嵌入来自 https://www.boost.org/doc/libs/1_75_0/doc/html/boost_asio/
我正在基于 windows io 完成端口的异步套接字类中添加对协程 ts 的支持。 如果没有协程,io 可以这样完成: sock.async_write(io::buffer(somebuff),
一个简单的函数: awaitable write(const std::vector& data) { auto token = co_await this_coro::token();
以下代码不符合我的预期。 #include #include #include struct symmetic_awaitable { std::coroutine_handle<> _
这个问题已经有答案了: Is it possible to co_await in a catch clause? (1 个回答) 已关闭去年。 我有一个如下所示的代码: auto func() ->
这个问题已经有答案了: Is it possible to co_await in a catch clause? (1 个回答) 已关闭去年。 我有一个如下所示的代码: auto func() ->
结构任务修改自https://github.com/Quuxplusone/coro/blob/master/include/coro/gor_task.h .我只是更改suspend_always
我有一个由 Asio 用 one io_context per thread 编写的 HTTP 服务器库模型。它有一个 io_context_pool检索一个 io_context有顺序地。所以当服务
说到 Boost Asio,我完全是个新手。我玩过回调,一切正常。但是,现在我正在尝试切换到协程,但我遇到了 co_await 的问题。 Visual Studio 2017 社区版说 “此 co_a
我已经安装了 windows 10.0 visual studio 2017(15.2 版)我已经为我的项目(包括 cpprestsdk)从 VS2013 迁移到 VS2017,并使用 co_awai
网上有一些链接 [1 , 2]建议 co_await 应该与 std::experimental::optional 一起使用,并且 VS 2015 Update 2 应该支持它。由于 VS 即使在
我是一名优秀的程序员,十分优秀!