Document Number: P1020R1
Date: 2018-11-06
Project: Programming Language C++
Audience: Library Working Group
Author: Glen Joseph Fernandes
(glenjofe@gmail.com),
Peter Dimov
(pdimov@pdimov.com)
This paper proposes adding the smart pointer creation functions
allocate_shared_default_init,
make_shared_default_init, and
make_unique_default_init that perform default initialization.
Minor changes to the proposed wording suggested in the LWG review.
It is not uncommon for arrays of built-in types such as unsigned
char or double to be immediately initialized by the user in
their entirety after allocation. In these cases, the value initialization
performed by allocate_shared, make_shared, and
make_unique is redundant and hurts performance, and a way to
choose default initialization is needed.
The Boost Smart Pointers
library has provided an implementation of this proposal since Boost 1.56
released in 2014. The Boost functions are named
allocate_shared_noinit, make_shared_noinit, and
make_unique_noinit, and are in widespread use.
All changes are relative to N4741.
Insert into 23.10.2 [memory.syn] as follows:
template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); //T is not array
template<class T> unique_ptr<T> make_unique(size_t n); //T is U[]
template<class T, class... Args> unspecified make_unique(Args&&...) = delete; //T is U[N]
template<class T> unique_ptr<T> make_unique_default_init(); //T is not array
template<class T> unique_ptr<T> make_unique_default_init(size_t n); //T is U[]
template<class T, class... Args> unspecified make_unique_default_init(Args&&...) = delete; //T is U[N]
[…]
template<class T> shared_ptr<T> make_shared(const remove_extent_t<T>& u); //T is U[N]
template<class T> shared_ptr<T> allocate_shared(const A& a, const remove_extent_t<T>& u); //T is U[N]
template<class T> shared_ptr<T> make_shared_default_init(); //T is not U[]
template<class T> shared_ptr<T> allocate_shared_default_init(const A& a); //T is not U[]
template<class T> shared_ptr<T> make_shared_default_init(size_t N); //T is U[]
template<class T> shared_ptr<T> allocate_shared_default_init(const A& a, size_t N); //T is U[]
Insert after 23.11.1.4 [unique.ptr.create] p5 as follows:
template<class T> unique_ptr<T> make_unique_default_init();Constraints:
Tis not an array.Returns:
unique_ptr<T>(new T).template<class T> unique_ptr<T> make_unique_default_init(size_t n);Constraints:
Tis an array of unknown bound.Returns:
unique_ptr<T>(new remove_extent_t<T>[n]).template<class T, class... Args> unspecified make_unique_default_init(Args&&...) = delete;Constraints:
Tis an array of known bound.
Change 23.11.3.6 [util.smartptr.shared.create] p1 as follows:
The common requirements that apply to all
make_shared,andallocate_shared,make_shared_default_init,andallocate_shared_default_initoverloads, unless specified otherwise, are described below.template<class T, ...> shared_ptr<T> make_shared(args);
template<class T, class A, ...> shared_ptr<T> allocate_shared(const A& a, args);
template<class T, ...> shared_ptr<T> make_shared_default_init(args);
template<class T, class A, ...> shared_ptr<T> allocate_shared_default_init(const A& a, args);
Change 23.11.3.6 [util.smartptr.shared.create] p3 as follows:
Effects: Allocates memory for an object of typeT(orU[N]whenTisU[], whereNis determined fromargsas specified by the concrete overload). The object is initialized fromargsas specified by the concrete overload. Theallocate_sharedandallocate_shared_default_inittemplates use a copy ofa(rebound for an unspecifiedvalue_type) to allocate memory. If an exception is thrown, the functions have no effect.
Insert after 23.11.3.6 [util.smartptr.shared.create] p7.7 as follows:
— When a (sub)object of non-array typeUis initialized bymake_shared_default_initorallocate_shared_default_init, it is initialized via the expression::new(pv) U, wherepvhas typevoid*and points to storage suitable to hold an object of typeU.
Insert after 23.11.3.6 [util.smartptr.shared.create] p22 as follows:
template<class T> shared_ptr<T> make_shared_default_init();
template<class T, class A> shared_ptr<T> allocate_shared_default_init(const A& a);Constraints:
Tis not an array of unknown bound.Returns: A
shared_ptrto an object of typeT.[Example:
struct X { double data[1024]; };
shared_ptr<X> p = make_shared_default_init<X>();
//shared_ptr to a default-initialized X, where each element in X::data has an indeterminate value
shared_ptr<double[1024]> q = make_shared_default_init<double[1024]>();
//shared_ptr to a default-initialized double[1024], where each element has an indeterminate value
—end example]template<class T> shared_ptr<T> make_shared_default_init(size_t N);
template<class T, class A> shared_ptr<T> allocate_shared_default_init(const A& a, size_t N);Constraints:
Tis an array of unknown bound.Returns: A
shared_ptrto an object of typeU[N], whereUisremove_extent_t<T>.[Example:
shared_ptr<double[]> p = make_shared_default_init<double[]>(1024);
//shared_ptr to a default-initialized double[1024], where each element has an indeterminate value
—end example]