Skip to content

Safe coroutine lambdas since C++23 using this auto #2326

@niekbouman

Description

@niekbouman

Dear cpp-cg,

I've placed a new comment about using a C++23 technique related to an old issue regarding coroutine lambdas.
However, since that issue is closed, I'm not sure if anyone actually noticed my comment, hence posting a link to that comment in this new issue:

#1756 (comment)

the gist is the following:

// Safe (C++23): The closure object is copied into the coroutine frame.
awaitable_task_type<int> compute_safe_cpp23() {
    int multiplier = 2;
    auto future = [multiplier](this auto /* keeps the captures alive */) -> awaitable_task_type<int> {
        co_await some_task_library::sleep(std::chrono::seconds(1));
        co_return multiplier * 21;
    }(); 
    
    co_return co_await std::move(future);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions