Home > Software engineering >  std::construct_at which default initializes?
std::construct_at which default initializes?

Time:11-04

std::construct_at is equivalent to

template<class T, class... Args>
constexpr T* construct_at( T* p, Args&&... args ) {
    return ::new (const_cast<void*>(static_cast<const volatile void*>(p)))
        T(std::forward<Args>(args)...);
}

except that construct_at may be used in evaluation of constant expressions.

As you can see, if no args are given, it will value initialize the object. Is there a way to achieve default initialization? To call the equivalent of ::new (p) T; (note the lack of parenthesis).

In other words, is it possible to execute default initialization with placement new which can be used in constant expressions?

CodePudding user response:

In other words, is it possible to execute default initialization with placement new which can be used in constant expressions?

No, not without further language support. This is why P2283 proposes adding default_construct_at to be a magic library function that lets you do this.

  • Related