C++ Logo

std-proposals

Advanced search

Re: [std-proposals] std::generator initial_suspend dropping all temporaries out of scope

From: karl3_at <karl3_at_[hidden]>
Date: Tue, 28 Jan 2025 22:53:56 +0100
> > 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

> It's certainly been known in some circles since forever. Here's a blog post
> I wrote on the subject in July 2019. Actually the *subject* of the blog
> post was an exciting new *cutting-edge* way to dangle a reference that I'd
> just learned; the way you're talking about here is what I called the
> "boring old way" back in 2019.
> https://quuxplusone.github.io/blog/2019/07/10/ways-to-get-dangling-references-with-coroutines/

Thank you. It's great to see a description somewhere and I'm glad the problem
is known.

I think it would make sense for your "Boring old way, redux" to succeed.

After considering your newer, more exotic way to cause this kind of issue I
feel like it should succeed too, but it does look like a different case.

> > Ideally there would be some way to configure this.

> I believe there is: it's "write your own generator type instead of using
> the one we standardized." Coroutines in C++ is a really low-level,
> not-for-ordinary-programmers kind of feature. It's probably not possible to
> "just use" coroutines in C++ in the way you can in, say, Python.

Attached is an initial patch I'm trying out to change the initial_suspend
behavior. The changes are few and hopefully let your redux approach be used to
provide generator interfaces that accept temporaries.
This patch applies onto:
        repo: https://github.com/lewissbaker/generator
        commit: e7c6c1c3009f0e9cf32add465b16f6295bfb9ac4
        file: include/__generator.hpp

Received on 2025-01-28 21:53:57