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

相关推荐
bbqz00710 天前
浅说 c++20 cppcoro (三)
c++·c++20·协程·coroutine·co_await·co_yield·cppcoro·co_return
Bender_ydc12 天前
使用 C++20 Modules 导入 Boost 模块的方法
c++20
fpcc12 天前
C++20中的std::atomic_ref
c++·c++20
whsyjgpg13 天前
已经安装了Visual C++ 2015-2022 Redistributable,但运行程序时,提示找不到VCRUNIME140_1D.dll
c++20
lucky-billy14 天前
C++20新特性
c++20
salsm1 个月前
使用 C++ 在深度学习中的应用:如何通过 C++20 构建高效神经网络
深度学习·神经网络·c++20
小小bugbug1 个月前
深度探索C++20协程机制
c++20
bbqz0071 个月前
浅说 c++20 coroutine
c++·c++20·协程·coroutine·co_await·stackless
arong_xu1 个月前
优雅处理任务取消: C++20 的 Cooperative Cancellation
多线程·c++20·线程取消
charlie1145141912 个月前
C++ STL CookBook
开发语言·c++·stl·c++20