指的是提案:P0914R1
gcc-14和clang-18都显示:只有MyCoroutinePromise::MyCoroutinePromise()可以用。
#include <coroutine>
#include <iostream>
struct MyClass;
struct MyCoroutine;
struct MyCoroutinePromise
{
MyClass* parent;
MyCoroutinePromise(MyClass* p) // <---
: parent(p)
{
std::cout << "Promise constructor called with parent: " << p << std::endl;
}
MyCoroutine get_return_object();
std::suspend_always initial_suspend() noexcept { return {}; }
std::suspend_always final_suspend() noexcept { return {}; }
void return_void() {}
void unhandled_exception() {}
};
struct MyCoroutine
{
using promise_type = MyCoroutinePromise;
std::coroutine_handle<promise_type> handle;
MyCoroutine(std::coroutine_handle<promise_type> h)
: handle(h)
{}
~MyCoroutine()
{
handle.destroy();
}
};
MyCoroutine MyCoroutinePromise::get_return_object()
{
return MyCoroutine{std::coroutine_handle<MyCoroutinePromise>::from_promise(*this)};
}
struct MyClass
{
MyCoroutine my_coroutine()
{
co_return;
}
};
int main()
{
MyClass obj;
obj.my_coroutine();
return 0;
}
--
修改:allegro FROM 61.188.76.*
FROM 61.188.76.*