#include <iostream>
#include "common/log.h"
#include <limits>
#include <vector>
#include <span>
#include <array>
#include <type_traits>
#include <cmath>
#include <memory>
#include <variant>
#include <cassert>
#include <concepts>
#include <ranges>
#include <coroutine>
#include <exception>
using 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