https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117374
Bug ID: 117374
Summary: Strange behavior of co_yield in initializer-list
Product: gcc
Version: 14.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: ddvamp007 at gmail dot com
Target Milestone: ---
#include <coroutine>
#include <iostream>
struct Promise;
using HandleBase = std::coroutine_handle<Promise>;
struct Promise {
HandleBase get_return_object() { return HandleBase::from_promise(*this); }
std::suspend_always initial_suspend() { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
void return_void() {}
void unhandled_exception() {}
int x = -1;
auto yield_value(int value) {
struct Awaiter {
int &slot;
int value;
Awaiter(int &r, int v) : slot(r), value(v) {
// slot = value;
}
bool await_ready() { return false; }
void await_suspend(HandleBase) {}
int await_resume() { return slot = value; }
};
return Awaiter(x, value);
}
};
struct Handle : HandleBase {
using promise_type = Promise;
Handle(HandleBase b) : HandleBase(std::move(b)) {}
int operator() () {
HandleBase::operator()();
return promise().x;
}
};
Handle Coro() {
int x = 0;
int arr[] = {co_yield x++, co_yield x++, co_yield x++};
std::cout << "In coro arr: [" << arr[0] << ", " << arr[1] << ", " << arr[2]
<< ']';
}
int main() {
auto gen = Coro();
int arr[] = {gen(), gen(), gen()};
auto last = gen();
std::cout << "\nIn main arr: [" << arr[0] << ", " << arr[1] << ", " <<
arr[2] << ']';
std::cout << "\nlast: " << last;
gen.destroy();
return 0;
}
In godbolt gcc 14.2 and gcc (trunk)
This code prints:
In coro arr: [0, 1, 2]
In main arr: [-1, -1, -1]
last: 2
If undo comment in Awaiter constructor:
In coro arr: [0, 1, 2]
In main arr: [2, 2, 2]
last: 2