std::allocator::construct(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… std::allocator::construct(3) πŸ’…

okay so bestie, welcome to the std::allocator::construct(3) page from the C++ Standard Library manual. this one is a little vintage, a little deprecated, and honestly kind of iconic in a β€œwe used to do it this way” way. let’s get into it πŸ’–


✨ NAME ✨

std::allocator::construct β€” std::allocator::construct (yes, the name is literally the name, periodt πŸ‘‘

πŸŽ€ Synopsis πŸŽ€

Defined in header <memory> β€” noted, bestie πŸ“Œ

void construct( pointer p, const_reference val ); (1) (until C++11)
template< class U, class... Args >                    (since C++11)
void construct( U* p, Args&&... args );           (2) (deprecated in C++17)
                                                          (removed in C++20)

okay so what does it do? it constructs an object of type T in allocated uninitialized storage pointed to by p, using global placement-new. the way that this is literally placement-new with extra steps is sending me 😭

  1. Calls ::new((void*)p) T(val).
  2. Calls ::new((void*)p) U(std::forward<Args>(args)...).
⚠️ heads up: overload (2) is deprecated in C++17 and removed in C++20 β€” not me being dramatic, that’s just the timeline 😭

🌸 Parameters 🌸

  • p β€” pointer to allocated uninitialized storage
  • val β€” the value to use as the copy constructor argument
  • args... β€” the constructor arguments to use

that’s the whole squad, no cap 🫢

πŸ’– Return value πŸ’–

(none) β€” literally nothing comes back, bestie. it’s giving void energy 🫢

🫢 Defect reports 🫢

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

  • LWG 402 β€” Applied to: C++98. Behavior as published: if T::operator new exists, the program might be ill-formed. Correct behavior: uses global replacement-new instead. πŸ”§

πŸ”₯ See also πŸ”₯

  • construct [static] β€” constructs an object in the allocated storage (function template)
  • construct_at (C++20) β€” creates an object at a given address (function template)
  • operator new β€” allocation functions
  • operator new[] β€” (function)

mwah πŸ’‹ β€” source: http://cppreference.com β€” version/date: 2024.06.10 β€” std::allocator::construct(3) πŸŽ€

πŸ“¦ where this page comes from