Giter VIP home page Giter VIP logo

Comments (5)

andreasfertig avatar andreasfertig commented on September 26, 2024

Hello @Ukilele,

I am currently learning about coroutines. And even if cppinsights claims to only be an approximation, it is a massive help! Thanks a lot 🎆

my pleasure!

What you report here is a very interesting case. What I struggle with is this part from your cppreference quote:

with post-copy coroutine arguments

my patch would make it look like what you're showing above. However, I wonder whether another interpretation is:

new (&__f->__promise)std::__coroutine_traits_sfinae<my_resumable>::promise_type{a, b, c};

This form would be broken for moved-from objects, so I assume your version is the correct one.

Andreas

from cppinsights.

Ukilele avatar Ukilele commented on September 26, 2024

Yes, I am pretty sure it is __f->a and not a. This gets also confirmed by https://lewissbaker.github.io/2022/08/27/understanding-the-compiler-transform. The exception to that rule is the *this object in the case that the coroutine is a non-static member function. See: dcl.fct.def.coroutine#4: If the coroutine is a non-static member function, then "q1 is an lvalue that denotes *this".

A minor nitpick: According to dcl.fct.def.coroutine#5.7, it should be (__f->a, __f->b, __f->c), so using parentheses instead of braces. But I don't know if there can be any observable difference between these two.

Thanks,
Kilian

from cppinsights.

andreasfertig avatar andreasfertig commented on September 26, 2024

Hello @Ukilele,

Yes, I am pretty sure it is __f->a and not a.

perfect.

The exception to that rule is the *this object in the case that the coroutine is a non-static member function. See: dcl.fct.def.coroutine#4: If the coroutine is a non-static member function, then "q1 is an lvalue that denotes *this".

That is an interesting exception. I'm not sure whether C++ Insights currently shows such a case correctly. Do you happen to have an example for this case?

Andreas

from cppinsights.

Ukilele avatar Ukilele commented on September 26, 2024

Here is a snippet, where we have a class ClassWithCoro and

  1. The class ClassWithCoro has a member function coro which is a coroutine (line 26) and
  2. The promise_type has a constructor (line 8) which takes a const ClassWithCoro& q1 (the lvalue that denotes *this) and a int& q2 (the remaining post-copy coroutine arguments).
#include <coroutine>
#include <utility>

struct ClassWithCoro;

struct my_resumable {
  struct promise_type {
    promise_type(const ClassWithCoro& q1, int& q2) {}
    my_resumable get_return_object() {
      return my_resumable(my_resumable::handle_type::from_promise(*this));
    }
    std::suspend_never initial_suspend() { return {}; }
    std::suspend_always final_suspend() noexcept { return {}; }
    void return_void() {}
    void unhandled_exception() {}
  };
  
  using handle_type = std::coroutine_handle<promise_type>;
  my_resumable(handle_type h) : m_handle(h) {}
  ~my_resumable() { if (m_handle) m_handle.destroy(); }
  my_resumable(my_resumable&& other) = delete;
  handle_type m_handle;
};

struct ClassWithCoro {
  my_resumable coro(int x) const {
    co_return;
  }
};

Like I said earlier, I am still learning about coroutines. But I would say that the transformation looks good to me, except of the issue at hand. I.e. that cppinsights generates

new (&__f->__promise)std::__coroutine_traits_impl<my_resumable>::promise_type{};

whereas instead it should generate:

new (&__f->__promise)std::__coroutine_traits_impl<my_resumable>::promise_type(*this, __f->x);

I hope my example is helpful :)

from cppinsights.

Ukilele avatar Ukilele commented on September 26, 2024

Thank you! 🚀 🙂

from cppinsights.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.