C++20rangesandcoroutines-创新互联

#include#include "common/log.h"
#include#include#include 
#include 
#include#include#include#include#include#include#include#include#includeusing namespace AdsonLib;
struct ReturnObject {
  struct promise_type {
    ReturnObject get_return_object() { return {}; }
    std::suspend_never initial_suspend() { return {}; }
    std::suspend_never final_suspend() noexcept { return {}; }
    void unhandled_exception() {}
  };
};

struct Awaiter {
  std::coroutine_handle<>*hp_;
  constexpr bool await_ready() const noexcept { return false; }
  void await_suspend(std::coroutine_handle<>h) { *hp_ = h; }
  constexpr void await_resume() const noexcept {}
};


ReturnObject counter(std::coroutine_handle<>*continuation_out)
{
  Awaiter a{continuation_out};
  for (unsigned i = 0;; ++i) {
    co_await a;
    std::cout<< "counter: "<< i<< std::endl;
  }
}
void test_co(){
    std::coroutine_handle<>h;
    counter(&h);
    for (int i = 0; i< 3; ++i) {
        std::cout<< "In main1 function\n";
        h();
    }
    h.destroy();
}
int main(int argc, char *argv[]) {
    LOG(INFO)<< "testing...";
    auto res = std::views::iota(1)
        | std::views::transform([](auto n){ return n * n; })
        | std::views::filter([](auto n) { return n % 2 == 1; })
        | std::views::take_while([](auto n){ return n< 10000; })
        ;
    for(auto n : res) {
        LOG(INFO)<< n;
    }
    std::string s = "hello, world";
    std::string_view sv = s;
    LOG(INFO)<< sv.substr(0, 5)<< " "<< sv.starts_with("he");
    test_co();

}

编译链接加上:"-fcoroutines"

创新互联服务项目包括土默特左旗网站建设、土默特左旗网站制作、土默特左旗网页制作以及土默特左旗网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,土默特左旗网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到土默特左旗省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

https://itnext.io/c-20-coroutines-complete-guide-7c3fc08db89d

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


当前名称:C++20rangesandcoroutines-创新互联
当前URL:http://azwzsj.com/article/dciedj.html