std::alloca...or_overwrite(3)
πΈ NAME πΈ
okay so bestie, this is the std::alloca...or_overwrite(3) page from the C++ Standard Libary manual β yes, thatβs literally the manual name, typo and all π
. Weβre talking about std::allocate_shared and std::allocate_shared_for_overwrite, the allocator-powered shared-pointer girlies β¨.
Names: std::allocate_shared,std::allocate_shared_for_overwrite β std::allocate_shared,std::allocate_shared_for_overwrite.
β¨ Synopsis β¨
Defined in header <memory> π
template< class T, class Alloc, class... Args >
shared_ptr<T> allocate_shared( const Alloc& alloc, Args&&... args );
template< class T, class Alloc >
shared_ptr<T> allocate_shared( const Alloc& alloc, std::size_t N );
template< class T, class Alloc >
shared_ptr<T> allocate_shared( const Alloc& alloc );
template< class T, class Alloc >
shared_ptr<T> allocate_shared( const Alloc& alloc, std::size_t N,
const std::remove_extent_t<T>& u );
template< class T, class Alloc >
shared_ptr<T> allocate_shared( const Alloc& alloc,
const std::remove_extent_t<T>& u );
template< class T, class Alloc >
shared_ptr<T> allocate_shared_for_overwrite( const Alloc& alloc );
template< class T, class Alloc >
shared_ptr<T> allocate_shared_for_overwrite( const Alloc& alloc,
std::size_t N );
Hereβs the overload map, because we love a numbered list:
- (1) since C++11;
Tis non-array. - (2) since C++20;
TisU[]. - (3) since C++20;
TisU[N]. - (4) since C++20;
TisU[]. - (5) since C++20;
TisU[N]. - (6) since C++20;
Tis notU[]. - (7) since C++20;
TisU[].
The overloads, explained π§
- (1) constructs an object of type
Tand wraps it in astd::shared_ptrusingargsas the parameter list for the constructor ofT. The object is constructed as if by the expression::new (pv) T(v)(until C++20) orstd::allocator_traits<A2>::construct(a, pv, v)(since C++20), wherepvis an internalvoid*pointer to storage suitable to hold an object of typeT, andais a copy of the allocator rebound tostd::remove_cv_t<T>. The storage is typically larger thansizeof(T)in order to use one allocation for both the control block of the shared pointer and theTobject. Thestd::shared_ptrconstructor called by this function enablesshared_from_thiswith a pointer to the newly constructed object of typeT. All memory allocation is done using a copy ofalloc, which must satisfy theAllocatorrequirements. This overload participates in overload resolution only ifTis not an array type. - (2,3) same as (1), but the object constructed is a possibly-multidimensional array whose every non-array element is initialized as if by the expression
std::allocator_traits<A2>::construct(a2, pv)wherea2of typeA2is the copy of the allocator rebound to manage objects of typestd::remove_cv_t<std::remove_all_extents_t<T>>. The overload (2) creates an array of sizeNalong its first dimension. The array elements are initialized in ascending order of their addresses, and when their lifetime ends are destroyed in the reverse order of their original construction. - (4,5) same as (2,3), but the elements of the array are initialized from the default value
u. Ifstd::remove_extent_t<T>is not itself an array type, then this is performed as if by the same allocator expression as in (1), except that the allocator is rebound to thestd::remove_cv_t<std::remove_all_extents_t<T>>. Otherwise, this is performed as if by initializing every non-array element of the (possibly multidimensional) array with the corresponding element fromuusing the same allocator expression as in (1), except that the allocator is rebound to the typestd::remove_cv_t<std::remove_all_extents_t<T>>. The overload (4) creates an array of sizeNalong the first dimension. The array elements are initialized in ascending order of their addresses, and when their lifetime ends are destroyed in the reverse order of their original construction. - (6) same as (1) if
Tis not an array type and (3) ifTisU[N], except that the created object is default-initialized. - (7) same as (2), except that the individual array elements are default-initialized.
For allocate_shared, the object (or the individual array elements for (2-5)) (since C++20) are destroyed via the expression std::allocator_traits<A2>::destroy(a, p), where p is a pointer to the object and a is a copy of the allocator passed to allocate_shared, rebound to the type of the object being destroyed.
For allocate_shared_for_overwrite, the object (or individual elements if T is an array type) will be destroyed by p->~X(), where p is a pointer to the object and X is its type (since C++20).
π Parameters π
allocβ theAllocatorto use.args...β list of arguments with which an instance ofTwill be constructed.Nβ array size to use.uβ the initial value to initialize every element of the array.
π¦ Return value π¦
std::shared_ptr of an instance of type T.
πΈ Exceptions πΈ
- Can throw the exceptions thrown from
Alloc::allocate()or from the constructor ofT. - If an exception is thrown, (1) has no effect.
- If an exception is thrown during the construction of the array, already-initialized elements are destroyed in reverse order (since C++20).
π Notes π
- Like
std::make_shared, this function typically performs only one allocation, and places both theTobject and the control block in the allocated memory block (the standard recommends but does not require this, all known implementations do this). A copy ofallocis stored as part of the control block so that it can be used to deallocate it once both shared and weak reference counts reach zero. - Unlike the
std::shared_ptrconstructors,std::allocate_shareddoes not accept a separate custom deleter: the supplied allocator is used for destruction of the control block and theTobject, and for deallocation of their shared memory block. std::shared_ptrsupports array types (as of C++17), butstd::allocate_shareddoes not. This functionality is supported by (until C++20)boost::allocate_shared.- A constructor enables
shared_from_thiswith a pointerptrof typeU*means that it determines ifUhas an unambiguous and accessible (since C++17) base class that is a specialization ofstd::enable_shared_from_this, and if so, the constructor evaluates the statement:
if (ptr != nullptr && ptr->weak_this.expired())
ptr->weak_this = std::shared_ptr<std::remove_cv_t<U>>(
*this, const_cast<std::remove_cv_t<U>*>(ptr));
Where weak_this is the hidden mutable std::weak_ptr member of std::enable_shared_from_this. The assignment to the weak_this member is not atomic and conflicts with any potentially concurrent access to the same object. This ensures that future calls to shared_from_this() would share ownership with the std::shared_ptr created by this raw pointer constructor.
- The test
ptr->weak_this.expired()in the exposition code above makes sure thatweak_thisis not reassigned if it already indicates an owner. This test is required as of C++17.
Feature-test macro β
__cpp_lib_smart_ptr_for_overwriteβ value202002L, std(C++20), feature: Smart pointer creation with default initialization (std::allocate_shared_for_overwrite,std::make_shared_for_overwrite,std::make_unique_for_overwrite); overloads (6,7).
π Example π
This section is incomplete Reason: no example
π See also π
constructorβ constructs newshared_ptr(public member function)make_sharedβ creates a shared pointer that manages a new objectmake_shared_for_overwriteβ (function template)(C++20)
π Category π
* Todo no example
mwah π source/version/date: http://cppreference.com, 2024.06.10, std::alloca...or_overwrite(3) β bye bestie, go allocate something cute π«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms