问题描述
我想知道如何将协程与通过引用捕获的临时协同等待是否有效.那么以下代码是否有效,我想编译器应该将临时变量和局部变量一样放在协程框架中:
I'm wondering whether co_awaiting a coroutine with a temporary captured by reference is valid or not. So is the following code valid, I guess should the compiler put the temporary in the coroutine frame as for local variables:
task g(const S&);
task f() {
co_await g(S{});
}
在clang和msvc( https://godbolt.org/z/HqHae8 )上,恢复g之后,无论在gcc上,临时文件似乎都被破坏了( https://godbolt.org/z/hFWvU-)似乎在临时挂起后临时文件被销毁了.
On clang and msvc (https://godbolt.org/z/HqHae8), the temporary seems to be detroyed after g is resumed, whether on gcc (https://godbolt.org/z/hFWvU-) it seems that the temporary is destroyed after initial suspend.
推荐答案
MSVC和Clang正确,临时表达式在完整表达式的末尾被销毁.
MSVC and Clang are right, temporaries are destroyed at the end of the full-expression.
对于GCC,这是错误 95599 .显然,此错误正在修复中.
For GCC, this is bug 95599. Apparently this bug is in the process of being fixed.
这篇关于co_await表达式中的c ++协程临时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!