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吗? 当然可以编译运行,就是编辑器总是提示错误。谁知道如何化解?

相关推荐
猿饵块7 天前
c++20--std::format
c++20
mrbone1117 天前
C++-关于协程的一些思考
开发语言·数据库·c++·c++20·协程·异步·coroutines
xiaolang_8616_wjl1 个月前
c++文字游戏_闯关打怪
开发语言·数据结构·c++·算法·c++20
十年编程老舅1 个月前
跨越十年的C++演进:C++20新特性全解析
c++·c++11·c++20·c++14·c++23·c++17·c++新特性
xiaolang_8616_wjl1 个月前
c++游戏_小恐龙(开源)
开发语言·数据结构·c++·算法·游戏·开源·c++20
a东方青2 个月前
[蓝桥杯C++ 2024 国 B ] 立定跳远(二分)
c++·算法·蓝桥杯·c++20
小葡萄20253 个月前
黑马程序员2024新版C++笔记 第五章 面向对象
开发语言·c++·笔记·c++20
Tipriest_3 个月前
【C++20新特性】ranges::sort()使用方法,优势,注意点
算法·leetcode·c++20·排序·sort
Tipriest_3 个月前
ubuntu20.04&vscode使用C++20(调整gcc版本&vscode设置)
ide·vscode·c++20·gcc
小葡萄20253 个月前
黑马程序员C++2024新版笔记 第4章 函数和结构体
笔记·c++20