为什么std::coroutine_handle类提供静态成员函数而不是构造对象的构造函数?

问题描述 投票:0回答:0

在我的

<coroutine>
标题中,我看到
std::coroutine_handle
除其他外,还有这两个构造函数,

constexpr coroutine_handle() noexcept { }
                                                   
constexpr coroutine_handle(nullptr_t) noexcept { }

和这个

static
成员函数,

static coroutine_handle
from_promise(_Promise& __p)
{
  coroutine_handle __self;
  __self._M_fr_ptr
    = __builtin_coro_promise((char*) &__p, __alignof(_Promise), true);
  return __self;
}

现在这个成员

from_promise
可以用来创建一个
coroutine_handle
_M_fr_ptr
成员设置为某物。

所以我想知道:为什么不用以下假设的构造器(对我来说,看起来像是在做同样的工作)而不是

from_promise

coroutine_handle(_Promise& __p)
  : _M_fr_ptr{__builtin_coro_promise((char*) &__p, __alignof(_Promise), true)}
{}

鉴于前一个解决方案的复杂性,我认为该决定不重要且有意识地做出的可能性为 0%。

c++ constructor c++20 static-methods c++-coroutine
© www.soinside.com 2019 - 2024. All rights reserved.