如何解决co_return vs. co_yield 当右手边是临时的
背景:当我阅读 cppcoro 的来源,特别是 this line 时,出现了这个问题。
问题:考虑以下代码:
#include <coroutine>
#include <stdexcept>
#include <cassert>
#include <iostream>
struct result_type {
result_type() {
std::cout << "result_type()\n";
}
result_type(result_type&&) noexcept {
std::cout << "result_type(result_type&&)\n";
}
~result_type() {
std::cout << "~result_type\n";
}
};
struct task;
struct my_promise {
using reference = result_type&&;
result_type* result;
std::suspend_always initial_suspend() { return {}; }
std::suspend_always final_suspend() noexcept {
return {};
}
task get_return_object();
void return_value(reference result_ref) {
result = &result_ref;
}
auto yield_value(reference result_ref) {
result = &result_ref;
return final_suspend();
}
void unhandled_exception() {}
};
struct task {
std::coroutine_handle<my_promise> handle{};
~task() {
if (handle) {
handle.destroy();
}
}
void run() {
handle.resume();
}
my_promise::reference result() {
return std::move(*handle.promise().result);
}
};
task my_promise::get_return_object() {
return { std::coroutine_handle<my_promise>::from_promise(*this) };
}
namespace std {
template <>
struct coroutine_traits<task> {
using promise_type = my_promise;
};
}
task f1() {
co_return result_type{};
}
task f2() {
co_yield result_type{};
// silence "no return_void" warning. This should never hit.
assert(false);
co_return result_type{};
}
int main() {
{
std::cout << "with co_return:\n";
auto t1 = f1();
t1.run();
auto result = t1.result();
}
std::cout << "\n==================\n\n";
{
std::cout << "with co_yield:\n";
auto t2 = f2();
t2.run();
auto result = t2.result();
}
}
在上面的代码中:
-
调用
f1()
和f2()
都会启动一个协程。协程立即挂起,一个包含协程句柄的task
对象返回给调用者。协程的promise 包含result
- 一个指向result_type
的指针。目的是让它在完成时指向协程的结果。 -
task.run()
在返回的任务上被调用,它恢复存储的协程句柄。 -
f1
和f2
的不同之处在于:-
f1
使用co_return result_type{}
对 Promise 调用return_value
。请注意,return_value
接受result_type
的 r 值引用,因此将其绑定到临时值。 -
f2
使用co_yield result_type{}
对 Promise 调用yield_value
。与return_value
相同,它接受一个 r 值- 此外,
yield_value
返回final_suspend()
,后者又返回std::suspend_always
,指示协程在产生值后挂起。
- 此外,
- 在
return_value
和yield_value
中,result
都设置为指向它们收到的参数。
-
然而,由于 final_suspend
也在 co_return
之后被调用(并等待其结果),我预计使用 co_return
和 co_yield
之间没有区别。但是,compiler proved me wrong:
with co_return:
result_type()
~result_type
result_type(result_type&&)
~result_type
==================
with co_yield:
result_type()
result_type(result_type&&)
~result_type
~result_type
请注意,在上面的输出中,co_return
版本构造一个结果,销毁它,然后从中移动构造,调用未定义的行为。但是,co_yield
版本似乎运行良好,仅在 移动构造之后破坏结果。
为什么这里的行为不同?
解决方法
您违反了 C++ 的基本规则:您编写了一个函数,该函数接受一个潜在的纯右值并存储一个指针,该指针比赋予它们一个纯右值的函数调用寿命更长。事实上,任何时候你看到一个函数接受一个右值引用(或一个常量左值引用)并存储一个指向该对象的指针/引用,该对象将超过该函数,你应该认为该代码高度 充其量是可疑的。
如果一个函数接受一个参数作为右值引用,这意味着您应该在该函数调用中使用它或从它移动。纯右值通常不会超过传递给它们的函数调用的寿命,因此您要么使用它们,要么丢失它们。
无论如何,您看到的行为正是您应该看到的。当协程发出 co_return
时,它...返回。这意味着协程块的主体已经退出。 return_value
在块仍然存在时被调用,但一旦调用完成,协程块及其所有自动变量(包括参数)就会消失。
这就是为什么从普通函数返回对自动变量的引用是个坏主意。这对 co_return
来说同样是个坏主意,即使您是间接引导对调用者的引用。
co_yield
版本有效(你仍然不应该这样做,因为这不是你应该如何对待纯右值,但它必须工作)因为 co_yield
语句本身被告知通过 yield_value
的返回值暂停。这会保留协程的堆栈,包括 co_yield
语句本身内的任何纯右值,直到协程恢复。
但同样,您应该在 yield_value
函数中进行移动,就像您通常对右值引用参数所做的那样。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。