Victor Burckel
Victor Burckel

Reputation: 183

c++ coroutines temporaries in co_await expressions

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{});
}

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.

Upvotes: 6

Views: 442

Answers (1)

Oliv
Oliv

Reputation: 18081

MSVC and Clang are right, temporaries are destroyed at the end of the full-expression.

For GCC, this is bug 95599. Apparently this bug is in the process of being fixed.

Upvotes: 6

Related Questions