23 General utilities library [utilities]

23.11 Smart pointers [smartptr]

23.11.2 Shared-ownership pointers [util.smartptr] Class template shared_­ptr [util.smartptr.shared] shared_­ptr creation [util.smartptr.shared.create]

template<class T, class... Args> shared_ptr<T> make_shared(Args&&... args); template<class T, class A, class... Args> shared_ptr<T> allocate_shared(const A& a, Args&&... args);

Requires: The expression ​::​new (pv) T(std​::​forward<Args>(args)...), where pv has type void* and points to storage suitable to hold an object of type T, shall be well formed. A shall be an allocator. The copy constructor and destructor of A shall not throw exceptions.

Effects: Allocates memory suitable for an object of type T and constructs an object in that memory via the placement new-expression ​::​new (pv) T(std​::​forward<Args>(args)...). The template allocate_­shared uses a copy of a to allocate memory. If an exception is thrown, the functions have no effect.

Returns: A shared_­ptr instance that stores and owns the address of the newly constructed object of type T.

Postconditions: get() != 0 && use_­count() == 1.

Throws: bad_­alloc, or an exception thrown from A​::​allocate or from the constructor of T.

Remarks: The shared_­ptr constructor called by this function enables shared_­from_­this with the address of the newly constructed object of type T. Implementations should perform no more than one memory allocation. [Note: This provides efficiency equivalent to an intrusive smart pointer. end note]

[Note: These functions will typically allocate more memory than sizeof(T) to allow for internal bookkeeping structures such as the reference counts. end note]