Search code examples
c++20coroutinevirtual-functionsc++-coroutine

Can virtual functions be C++20 coroutines?


The title says it all: Can virtual functions also be C++20 coroutines?

struct A {
    virtual std::generator<int> generate() const = 0; // std::generator: C++23
};

struct B : A {
    virtual std::generator<int> generate() const { ~~~~ }
};

?


Solution

  • Yes.

    From cppreference

    Restrictions
    Coroutines cannot use variadic arguments, plain return statements, or placeholder return types (auto or Concept).
    Constexpr functions, constructors, destructors, and the main function cannot be coroutines.

    Knowing how they work, I don't see any reason why it wouldn't be allowed.