And*_* H. 7 c++ g++ alloca c++20 c++-coroutine
GCC C++ 编译器(以及许多其他 C++ 编译器)提供非标准扩展,例如
alloca() 基于堆栈的分配从基本的角度来看,这些可以在 C++20 协程中使用吗?有可能吗?如果是,这是如何实施的?
据我了解,C++20 协程通常在第一次调用时(即创建承诺对象时)为协程创建堆栈帧,因此需要知道协程堆栈帧的大小。
然而,这在 alloca 或其他运行时动态堆栈分配中并不能很好地发挥作用。
那么是否有可能,如果有,它是如何实施的?或者有什么影响?
不幸的是,alloca与 GCC 中的 C++20 协程不兼容。最糟糕的是编译器不会对此发出警告。
此代码示例演示了不兼容性:
#include <coroutine>
#include <iostream>
struct ReturnObject {
struct promise_type {
unsigned * value_ = nullptr;
void return_void() {}
ReturnObject get_return_object() {
return {
.h_ = std::coroutine_handle<promise_type>::from_promise(*this)
};
}
std::suspend_never initial_suspend() { return {}; }
std::suspend_never final_suspend() noexcept { return {}; }
void unhandled_exception() {}
};
std::coroutine_handle<promise_type> h_;
operator auto() const { return h_; }
};
template<typename PromiseType>
struct GetPromise {
PromiseType *p_;
bool await_ready() { return false; }
bool await_suspend(std::coroutine_handle<PromiseType> h) {
p_ = &h.promise();
return false;
}
PromiseType *await_resume() { return p_; }
};
ReturnObject counter()
{
auto pp = co_await GetPromise<ReturnObject::promise_type>{};
//unsigned a[1]; auto & i = a[0]; //this version works fine
auto & i = *new (alloca(sizeof(unsigned))) unsigned(0); //and this not
for (;; ++i) {
pp->value_ = &i;
co_await std::suspend_always{};
}
}
int main()
{
std::coroutine_handle<ReturnObject::promise_type> h = counter();
auto &promise = h.promise();
for (int i = 0; i < 5; ++i) {
std::cout << *promise.value_ << std::endl;
h();
}
h.destroy();
}
Run Code Online (Sandbox Code Playgroud)
https://gcc.godbolt.org/z/8zG446Esx
它应该打印0 1 2 3 4,但不完全是由于使用alloca