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

相关推荐
郭涤生6 小时前
Chapter 5: The Standard Library (C++20)_《C++20Get the details》_notes
开发语言·c++·笔记·c++20
oioihoii4 天前
深入解析 C++20 中的 std::bind_front:高效函数绑定与参数前置
java·算法·c++20
oioihoii4 天前
C++20:make_shared_for_overwrite与make_unique_for_overwrite
jvm·算法·c++20
oioihoii6 天前
C++20 中的std::c8rtomb和 std::mbrtoc8
前端·c++20
不会写代码的工科狗7 天前
C++17和C++20引入的新特性
c++·算法·c++20
oioihoii7 天前
C++20 中 `constexpr` 的强大扩展:算法、工具与复数库的变革
算法·c++20
oioihoii9 天前
C++20:玩转 string 的 starts_with 和 ends_with
算法·c++20
oioihoii11 天前
C++20 新特性:深入理解 `std::basic_string<char8_t>` 和 `char8_t`
java·前端·c++20
oioihoii13 天前
C++20 中的同步输出流:`std::basic_osyncstream` 深入解析与应用实践
c++·算法·c++20
oioihoii13 天前
C++20 中线程管理与取消机制的深度剖析
java·jvm·c++20