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

相关推荐
郭涤生5 天前
Concepts (C++20)
c++20
chendilincd6 天前
C++ 的史诗级进化:从C++98到C++20
java·c++·c++20
oioihoii22 天前
C++20 统一容器擦除:std::erase 和 std::erase_if
c++20
郭涤生22 天前
The whole book test_《C++20Get the details》_notes
开发语言·c++·笔记·c++20
郭涤生22 天前
Chapter 6: Concurrency in C++20_《C++20Get the details》_notes
开发语言·c++·笔记·c++20
__lost23 天前
C++20的协程简介
c++20·协程
BanyeBirth23 天前
C++2025年3月等级考试试题(部分)
c++20
点云SLAM24 天前
C++20新增内容
c++·算法·c++20·c++ 标准库
oioihoii1 个月前
C++20 的新工具:std::midpoint 和 std::lerp
c++20
郭涤生1 个月前
Chapter 1: Historical Context_《C++20Get the details》_notes
开发语言·c++20