https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95540

            Bug ID: 95540
           Summary: [coroutine] coroutine_traits<> lookup for lambdas
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bruck.michael at gmail dot com
  Target Milestone: ---

This fails on gcc and clang

gcc: unable to find the promise type for this coroutine
clang: ...coroutine_traits<.., const (lambda at <source>:30:12) &>' has no
member named 'promise_type'

https://gcc.godbolt.org/z/kXzEZW

The clang error hints that the lookup is performed with P1 being the lambda
closure type. I suspect gcc does the same (there is potential for a more
detailed diagnostic here). This makes it impossible to use coroutine_traits<>
with lambdas.

n4835:
[dcl.fct.def.coroutine]
"The promise type of a coroutine is std::coroutine_traits<R, P1, . . . ,
Pn>::promise_type, where R is the return type of the function, and P1 . . . Pn
are the sequence of types of the function parameters, preceded by the type of
the implicit object parameter (12.4.1) if the coroutine is a non-static member
function. The promise type shall be a class type."

While a lambda does have a non-static member operator() the lambda expression
creates a closure object, not a member function. In any case either the
standard or the interpretation of the compilers prevents the use of
coroutine_traits<> for lambdas without a discernible benefit.


#ifndef __clang__
#include <coroutine>
#else
#include <experimental/coroutine>
namespace std { using namespace experimental; }
#endif

#include <cstdio>

struct pt
{
    using handle_t = std::coroutine_handle<pt>;
    auto get_return_object() noexcept { return handle_t::from_promise(*this); }

    std::suspend_never initial_suspend() const noexcept { return {}; }
    std::suspend_never final_suspend() const noexcept { return {}; }
    void return_void() const noexcept {}
    void unhandled_exception() const noexcept {}
};

template <> struct std::coroutine_traits<pt::handle_t>
    { using promise_type = pt; };

static pt::handle_t foo()
{ 
    printf("from function\n");
    co_return;
}

auto bar = []() -> pt::handle_t
{ 
    printf("from lambda\n");
    co_return;
};

int main()
{
    foo();
    bar();
}

Reply via email to