Date: Wed, 29 Jan 2025 07:13:49 +0000
Wouldn’t this problem be better solved with move semantics?
From: Std-Proposals <std-proposals-bounces_at_lists.isocpp.org> On Behalf Of Lénárd Szolnoki via Std-Proposals
Sent: Wednesday, January 29, 2025 7:38 AM
To: std-proposals_at_[hidden]
Cc: Lénárd Szolnoki <cpp_at_lenardszolnoki.com>
Subject: Re: [std-proposals] std::generator initial_suspend dropping all temporaries out of scope
You can have a wrapper function (not coroutine) that returns std::generator.
std::generator<T> wrapper(ReferenceType x) {
return [](ValueType x)->std::generator<T>{
/*implement coroutine*/
}(DeepCopy(x));
}
DeepCopy(x) is evaluated before the initial suspend and its result lives on the coroutine frame.
On 28 January 2025 18:43:20 GMT, Karl Semich via Std-Proposals <std-proposals_at_[hidden]<mailto:std-proposals_at_[hidden]>> wrote:
Hi!
I'm trying out c++23 std::generator and noticing that I cannot safely pass arguments to my generators that are temporaries such as std::initializer_list. It seems like this is because promise_type's initial_suspend returns suspend_always, so passed temporaries are out of scope as soon as initial code is executed.
Where can I read about this and best practices, or is it a mistake? It looks like a mistake to me.
My understanding is that if initial_suspend returned suspend_never, then the developer could accept temporaries and copy their contents to the stack in the initial code block of the generator. But as it is, there can be surprising subtle memory access errors in code not aware of the limitation that generators cannot safely accept temporaries at all.
Ideally there would be some way to configure this.
Karl Semich
From: Std-Proposals <std-proposals-bounces_at_lists.isocpp.org> On Behalf Of Lénárd Szolnoki via Std-Proposals
Sent: Wednesday, January 29, 2025 7:38 AM
To: std-proposals_at_[hidden]
Cc: Lénárd Szolnoki <cpp_at_lenardszolnoki.com>
Subject: Re: [std-proposals] std::generator initial_suspend dropping all temporaries out of scope
You can have a wrapper function (not coroutine) that returns std::generator.
std::generator<T> wrapper(ReferenceType x) {
return [](ValueType x)->std::generator<T>{
/*implement coroutine*/
}(DeepCopy(x));
}
DeepCopy(x) is evaluated before the initial suspend and its result lives on the coroutine frame.
On 28 January 2025 18:43:20 GMT, Karl Semich via Std-Proposals <std-proposals_at_[hidden]<mailto:std-proposals_at_[hidden]>> wrote:
Hi!
I'm trying out c++23 std::generator and noticing that I cannot safely pass arguments to my generators that are temporaries such as std::initializer_list. It seems like this is because promise_type's initial_suspend returns suspend_always, so passed temporaries are out of scope as soon as initial code is executed.
Where can I read about this and best practices, or is it a mistake? It looks like a mistake to me.
My understanding is that if initial_suspend returned suspend_never, then the developer could accept temporaries and copy their contents to the stack in the initial code block of the generator. But as it is, there can be surprising subtle memory access errors in code not aware of the limitation that generators cannot safely accept temporaries at all.
Ideally there would be some way to configure this.
Karl Semich
Received on 2025-01-29 07:13:53