C++20-协程

昨天看到一本书,《现代C++语言核心特性解析》,第33章是协程,我机器上安装了vs2022,肯定是支持的,直接运行第一个例子就报错了。

cpp 复制代码
#include <iostream>
#include <chrono>
#include <future>

using namespace std::chrono_literals;

std::future<int> foo()
{
  std::cout << "call foo\n";
    std::this_thread::sleep_for(3s);
    co_return 5;
}

std::future<std::future<int>> bar()
{
  std::cout << "call bar\n";
  std::cout << "before foo\n";
  auto n = co_await std::async(foo);       // 挂起点
  std::cout << "after foo\n";
  co_return n;
}

int main()
{
  std::cout << "before bar\n";
  auto i = bar();
  std::cout << "after bar\n";
    i.wait();
  std::cout << "result = " << i.get().get();
}

显示少了#include <coroutine>,加上后不行,又把项目的C++语言设置为C++20,缺省是C++14。

然后报如下错误:

错误 C2039 "promise_type": 不是 "std::coroutine_traits<std::future<int>>" 的成员 testco C:\cpp\vc2022\testco\testco\co1.cpp 13

"promise_type": is not member of "std::experimental::coroutine_traits<void>

搜了一大圈不明就里,现在搜索质量真是差。

暂时放弃了。

晚上在B站浏览协程相关内容,发现一哥们讲的通俗易懂,就去了他的代码:

cpp 复制代码
#include <coroutine>
#include <iostream>
#include <chrono>
#include <future>
#include <thread>

using namespace std::chrono_literals;

void Fun() {
    std::cout << 1 << std::endl;
    std::cout << 2 << std::endl;
    std::cout << 3 << std::endl;
    std::cout << 4 << std::endl;
}

struct Result {
    struct promise_type {
        std::suspend_never initial_suspend() {
            return {};
        }

        std::suspend_never final_suspend() noexcept {
            return {};
        }

        Result get_return_object() {
            return {};
        }

        void return_void() {

        }
        void unhandled_exception() {

        }
    };
};

struct Awaiter {
    int value;

    bool await_ready() {
        return false;
    }

    void await_suspend(std::coroutine_handle<> coroutine_handle) {
        std::async([=]() {
            std::this_thread::sleep_for(1s);
            coroutine_handle.resume();
            });
    }

    int await_resume() {
        return value;
    }
};

Result Coroutine() {
    std::cout << 1 << std::endl;
    std::cout << co_await Awaiter{ .value = 1000 } << std::endl;
    std::cout << 2 << std::endl;
    std::cout << 3 << std::endl;
    co_await std::suspend_always{};
    std::cout << 4 << std::endl;

    co_return;
};

int main() {
    Coroutine();
    return 0;
}

这个代码可以跑,找到了这个兄弟的网站,慢慢了解一下C++协程

1. C++ 协程概览 | Benny Huo 的专栏

当然还有个比较令人烦闷的事情,VS2022里面,std::suspend_never,std::coroutine_handle,co_await编辑器都标识为错误,这是为啥,VS2022不是号称支持C++20吗? 当然可以编译运行,就是编辑器总是提示错误。谁知道如何化解?

相关推荐
炫酷的伊莉娜3 天前
C++20 std::format
c++20
Trouvaille ~7 天前
【C++篇】C++类与对象深度解析(六):全面剖析拷贝省略、RVO、NRVO优化策略
c++·c++20·编译原理·编译器·类和对象·rvo·nrvo
martian6659 天前
学懂C++(六十):C++ 11、C++ 14、C++ 17、C++ 20新特性大总结(万字详解大全)
开发语言·c++·c++20
班公湖里洗过脚12 天前
标准库标头 <bit>(C++20)学习
c++20
jianglq13 天前
C++20 协程:异步编程的新纪元
算法·c++20
zhenghe1236513 天前
c++20 std::format 格式化说明
c++20
fengbingchun13 天前
C++20中支持的非类型模板参数
c++20
jianglq14 天前
C++20标准对线程库的改进:更安全、更高效的并发编程
c++·c++20
jianglq14 天前
C++20 新特征:Ranges库初探
开发语言·c++20·c++20新特征