C++ 如何用C++;20个协同程序?

C++ 如何用C++;20个协同程序?,c++,generator,c++20,c++-coroutine,C++,Generator,C++20,C++ Coroutine,出于学习的目的,我尝试使用C++20协同程序制作过于复杂的“Hello World”程序: HelloWorldMessage sayHelloToWorld() { co_yield "Hello"; co_yield " "; co_yield "World"; co_yield "!"; } int main() { for (auto w : sayHelloToWorld()) { std::cout <<

出于学习的目的,我尝试使用C++20协同程序制作过于复杂的“Hello World”程序:

HelloWorldMessage sayHelloToWorld()
{
    co_yield "Hello";
    co_yield " ";
    co_yield "World";
    co_yield "!";
}

int main() 
{
    for (auto w : sayHelloToWorld())
    {
        std::cout << w;
    }
}
也许叮当声还没准备好?

有几个错误:

第一-承诺应返回生成器对象,而不是引用自身。因此,正确的方法是:

struct HelloWorldPromise
{
    ...
    auto get_return_object();
    ...        
};

struct HelloWorldMessage
{
    ...
};

auto HelloWorldPromise::get_return_object()
{
    return HelloWorldMessage(*this);
}
下一步-终止和返回无效可以简化为:

void return_void() noexcept
{}  
void unhandled_exception()
{
    std::terminate();
}
接下来-在迭代器中-我们将依赖于
handle.done
-因此不需要
状态.finalWord
。完整迭代器源是:

struct Iter
{
    promise_handle handle = nullptr;
    HelloWorldState state;

    reference operator * () const { return state.currentWord; }
    pointer operator -> () const { return std::addressof(operator*()); }

    bool operator == (const Iter& other) const { return !handle == !other.handle; }
    bool operator != (const Iter& other) const { return !(*this == other); }

    Iter() = default;
    Iter(promise_handle handle)
        : handle(handle)
    {
       next();
    }

    Iter& operator ++()
    {
        if (!handle)
            return *this;
        next();
        return *this;
    }

    void next()
    {
        if (!handle)
            return;
        try {
            handle.resume();
            if (!handle.done())
               state = handle.promise().state;
            else {
                handle = nullptr;
            }
        } catch (...) {
            std::cerr << "@%$#@%#@$% \n";
        }
    }

};
struct Iter
{
promise_handle handle=nullptr;
HelloWorldState;
引用运算符*()常量{return state.currentWord;}
指针运算符->()常量{return std::addressof(运算符*());}
布尔运算符==(const Iter&other)const{return!handle==!other.handle;}
布尔运算符!=(const Iter&other)const{return!(*this==other);}
Iter()=默认值;
国际热核聚变实验堆(promise_handle)
:句柄(handle)
{
next();
}
Iter&operator++()
{
如果(!句柄)
归还*这个;
next();
归还*这个;
}
作废下一页()
{
如果(!句柄)
返回;
试一试{
handle.resume();
如果(!handle.done())
state=handle.promise().state;
否则{
handle=nullptr;
}
}捕获(…){

std::cerr完整的工作示例现在不起作用。您必须
#包括
。此外,还有一个警告,可以通过删除
HelloWorldMessage::Iter::operator=
(这需要编辑
operator!=
的定义)来消除@JohnFreeman谢谢,修复了。运算符==和!=没有常量说明符。
struct Iter
{
    promise_handle handle = nullptr;
    HelloWorldState state;

    reference operator * () const { return state.currentWord; }
    pointer operator -> () const { return std::addressof(operator*()); }

    bool operator == (const Iter& other) const { return !handle == !other.handle; }
    bool operator != (const Iter& other) const { return !(*this == other); }

    Iter() = default;
    Iter(promise_handle handle)
        : handle(handle)
    {
       next();
    }

    Iter& operator ++()
    {
        if (!handle)
            return *this;
        next();
        return *this;
    }

    void next()
    {
        if (!handle)
            return;
        try {
            handle.resume();
            if (!handle.done())
               state = handle.promise().state;
            else {
                handle = nullptr;
            }
        } catch (...) {
            std::cerr << "@%$#@%#@$% \n";
        }
    }

};