godbolt: https://godbolt.org/z/6avWcGqKv
The following code, compiled and run with g++ 12, 13, 14, all give the same (wrong?) output.
clang 18, 19 are fine (all "done: 1").
As far as I understand, since the initial_suspend() is suspend_never, after creation, the promise runs immediately to final_suspend, and should return "true" when querying "done()", but one of them does not.
Is it my programming error, or compiler error, or some UB, or something?
Program returned: 0
done: 1
done: 1
done: 1
done: 1
done: 1
done: 0 <-- ???
done: 1
done: 1
done: 1
done: 1
code:
#include <coroutine>
#include <iostream>
#include <vector>
struct Task {
struct promise_type {
Task get_return_object() {
return {.h = std::coroutine_handle<>::from_address(this)};
}
std::suspend_never initial_suspend() noexcept { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
void unhandled_exception() {}
void return_void() {}
};
std::coroutine_handle<> h;
};
Task coro() { co_return; }
int main() {
std::vector<Task> v;
for (int i = 0; i < 10; ++i)
v.push_back(coro());
for (auto &t : v)
std::cout << "done: "<< t.h.done() << "\n";
return 0;
}
std::coroutine_handle<>::from_address(this)is correct? cppreference says it must be an address of a handle, you are passing address ofpromise_type? This example usesfrom_promisein your context.from_address: The behavior is undefined if addr is neither a null pointer value nor an underlying address of a coroutine_handle. en.cppreference.com/w/cpp/coroutine/coroutine_handle/…