8

GCC C++ 编译器(也包括许多其他 C++ 编译器)提供非标准扩展,例如

  • alloca()基于堆栈的分配
  • 可变长度数组,因为它们是 C 标准的一部分

从基本的角度来看,这些可以在 C++20 协程中使用吗?有可能吗?如果是的话,这是如何实现的?

据我了解,C++20 协程通常会在第一次调用时(即创建承诺对象时)为协程创建堆栈帧,因此需要知道协程堆栈帧的大小。

然而,这不能很好地与 alloca 或其他运行时动态堆栈分配一起使用。

那么有没有可能,如果有,它是如何实施的?或者有什么影响?

4

1 回答 1

2

不幸的是,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();
}

https://gcc.godbolt.org/z/8zG446Esx

它应该打印0 1 2 3 4,但由于使用了alloca

于 2021-07-15T21:05:42.780 回答