如何解决使用协程在 C++ 中创建一个生成器
按照McNellis关于协程的介绍(C协程介绍),我尝试编译简单协程的例子,它实际上是一个简单的计数器。 然而,当简历被调用时,断言被触发。 具体
_LIBCPP_ASSERT(!done(),“当协程完成时,resume() 有未定义的行为”);
我对协程没有太多经验,我无法找出问题所在。
代码如下
import { screen } from '@testing-library/react';
...
const checkboxes = screen.getAllByRole('checkbox');
expect(checkboxes[0]).toBeChecked();
expect(checkboxes[0]).toHaveStyle(`
background-color: *your_value*;
width: *your_value*;
height: *your_value*;
`);
请找到代码here
解决方法
promise_type::get_return_object()
应返回用户定义的协程结果类型 resumable_thing
,而不是 promise_type&
本身。
struct resumable_thing
{
struct promise_type
{
resumable_thing get_return_object() noexcept
{
return {*this};
}
// ...
};
resumable_thing(promise_type & promise)
: _coroutine(coroutine_handle<promise_type>::from_promise(promise)) {}
// ...
};
并且 resumable_thing
的移动赋值运算符无意中重置了 this->_coroutine
。
// FIXED: resumable_thing const && -> resumable_thing &&
resumable_thing & operator = (resumable_thing && other)
{
_coroutine = other._coroutine;
// FIXED: _coroutine -> other._coroutine
other._coroutine = nullptr;
return *this;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。