Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/pr108620.C

raw
1 // { dg-skip-if "requires hosted libstdc++ for iostream" { ! hostedlib } }
2
3 // https://gcc.gnu.org/PR108620
4 #include <iostream>
5 #include <memory>
6 #include <coroutine>
7
8 template<class PrivateDataType>
9 struct task;
10
11 template <class PrivateDataType>
12 struct task_private_data {
13 inline task_private_data() noexcept : data_(nullptr) {}
14 inline task_private_data(PrivateDataType* input) noexcept : data_(input) {}
15 inline task_private_data(task_private_data&& other) noexcept = default;
16 inline task_private_data& operator=(task_private_data&&) noexcept = default;
17 inline task_private_data(const task_private_data&) = delete;
18 inline task_private_data& operator=(const task_private_data&) = delete;
19 inline ~task_private_data() {}
20
21 inline bool await_ready() const noexcept { return true; }
22 inline PrivateDataType* await_resume() const noexcept { return data_; }
23 inline void await_suspend(std::coroutine_handle<>) noexcept {}
24
25 PrivateDataType* data_;
26 };
27
28 template<class PrivateDataType>
29 struct task_context {
30 PrivateDataType data_;
31 };
32
33 template<class PrivateDataType>
34 struct task {
35 using self_type = task<PrivateDataType>;
36 std::shared_ptr<task_context<PrivateDataType>> context_;
37
38 task(const std::shared_ptr<task_context<PrivateDataType>>& input): context_(input) {}
39
40 static auto yield_private_data() noexcept { return task_private_data<PrivateDataType>{}; }
41
42 struct promise_type {
43 std::shared_ptr<task_context<PrivateDataType>> context_;
44
45 template<class Input, class... Rest>
46 promise_type(Input&& input, Rest&&...) {
47 context_ = std::make_shared<task_context<PrivateDataType>>();
48 context_->data_ = std::forward<Input>(input);
49 }
50
51 auto get_return_object() noexcept { return self_type{context_}; }
52 std::suspend_never initial_suspend() noexcept { return {}; }
53 std::suspend_never final_suspend() noexcept { return {}; }
54 void unhandled_exception() { throw; }
55
56 template<class ReturnType>
57 void return_value(ReturnType&&) {}
58
59 template <class InputPrivateDataType>
60 inline task_private_data<InputPrivateDataType> yield_value(
61 task_private_data<InputPrivateDataType>&& input) noexcept {
62 input.data_ = &context_->data_;
63 return task_private_data<InputPrivateDataType>(input.data_);
64 }
65 };
66 };
67
68 template<class TArg, class OutputType>
69 task<std::string> call1(TArg&& arg, OutputType& output) {
70 OutputType* ptr = co_yield task<TArg>::yield_private_data();
71 output = *ptr;
72 co_return 0;
73 }
74
75
76 struct container {
77 std::string* ptr;
78 };
79
80 template<class TArg>
81 task<std::string> call2(TArg&& arg, container& output) {
82 output.ptr = co_yield task<TArg>::yield_private_data();
83 co_return 0;
84 }
85
86 int main() {
87 // success
88 std::string output1;
89 call1(std::string("hello1"), output1);
90 std::cout<< "output1: "<< output1<< std::endl;
91
92 // crash
93 container output2;
94 auto task2 = call2(std::string("hello2"), output2);
95 std::cout<< "output2: "<< *output2.ptr<< std::endl;
96 return 0;
97 }