File: coro_test_code.cpp

package info (click to toggle)
range-v3 0.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,652 kB
  • sloc: cpp: 76,839; xml: 226; sh: 89; python: 34; makefile: 19; perl: 15
file content (46 lines) | stat: -rw-r--r-- 1,137 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#if defined(__cpp_coroutines) && defined(__has_include)
#if __has_include(<coroutine>)
#include <coroutine>
namespace std_coro = std;
#elif __has_include(<experimental/coroutine>)
#include <experimental/coroutine>
namespace std_coro = std::experimental;
#else
#error Either the compiler or the library lacks support for coroutines
#endif
#else
#error Either the compiler or the library lacks support for coroutines
#endif

struct present
{
    struct promise_type
    {
        int result;

        present get_return_object() { return {*this}; }
        std_coro::suspend_never initial_suspend() { return {}; }
        std_coro::suspend_never final_suspend() noexcept { return {}; }
        void return_value(int i) { result = i; }
        void unhandled_exception() {}
    };

    promise_type& promise;

    bool await_ready() const { return true; }
    void await_suspend(std_coro::coroutine_handle<>) const {}
    int await_resume() const { return promise.result; }
};

present f(int n)
{
    if (n < 2)
        co_return 1;
    else
        co_return n * co_await f(n - 1);
}

int main()
{
    return f(5).promise.result != 120;
}