|
9165 | 9165 |
|
9166 | 9166 | // \ref{util.smartptr.shared.create}, \tcode{shared_ptr} creation
|
9167 | 9167 | template<class T, class... Args>
|
9168 |
| - shared_ptr<T> make_shared(Args&&... args); |
| 9168 | + shared_ptr<T> make_shared(Args&&... args); // \tcode{T} is not array |
9169 | 9169 | template<class T, class A, class... Args>
|
9170 |
| - shared_ptr<T> allocate_shared(const A& a, Args&&... args); |
| 9170 | + shared_ptr<T> allocate_shared(const A& a, Args&&... args); // \tcode{T} is not array |
| 9171 | + |
| 9172 | + template<class T> shared_ptr<T> make_shared(size_t N); // \tcode{T} is \tcode{U[]} |
| 9173 | + template<class T, class A> |
| 9174 | + shared_ptr<T> allocate_shared(const A& a, size_t N); // \tcode{T} is \tcode{U[]} |
| 9175 | + |
| 9176 | + template<class T> shared_ptr<T> make_shared(); // \tcode{T} is \tcode{U[N]} |
| 9177 | + template<class T, class A> |
| 9178 | + shared_ptr<T> allocate_shared(const A& a); // \tcode{T} is \tcode{U[N]} |
| 9179 | + |
| 9180 | + template<class T> |
| 9181 | + shared_ptr<T> make_shared(size_t N, const remove_extent_t<T>& u); // \tcode{T} is \tcode{U[]} |
| 9182 | + template<class T, class A> |
| 9183 | + shared_ptr<T> allocate_shared(const A& a, size_t N, |
| 9184 | + const remove_extent_t<T>& u); // \tcode{T} is \tcode{U[]} |
| 9185 | + |
| 9186 | + template<class T> shared_ptr<T> |
| 9187 | + make_shared(const remove_extent_t<T>& u); // \tcode{T} is \tcode{U[N]} |
| 9188 | + template<class T, class A> |
| 9189 | + shared_ptr<T> allocate_shared(const A& a, |
| 9190 | + const remove_extent_t<T>& u); // \tcode{T} is \tcode{U[N]} |
9171 | 9191 |
|
9172 | 9192 | // \ref{util.smartptr.shared.cmp}, \tcode{shared_ptr} comparisons
|
9173 | 9193 | template<class T, class U>
|
|
9730 | 9750 |
|
9731 | 9751 | \rSec4[util.smartptr.shared.create]{\tcode{shared_ptr} creation}
|
9732 | 9752 |
|
| 9753 | +\pnum |
| 9754 | +The common requirements that apply to |
| 9755 | +all \tcode{make_shared} and \tcode{allocate_shared} overloads, |
| 9756 | +unless specified otherwise, are described below. |
| 9757 | + |
| 9758 | +\indexlibrary{\idxcode{make_shared}}% |
| 9759 | +\indexlibrary{\idxcode{allocate_shared}}% |
| 9760 | +\begin{itemdecl} |
| 9761 | +template<class T, ...> |
| 9762 | + shared_ptr<T> make_shared(\placeholder{args}); |
| 9763 | +template<class T, class A, ...> |
| 9764 | + shared_ptr<T> allocate_shared(const A& a, \placeholder{args}); |
| 9765 | +\end{itemdecl} |
| 9766 | + |
| 9767 | +\begin{itemdescr} |
| 9768 | +\pnum |
| 9769 | +\requires \tcode{A} shall be an allocator~(\ref{allocator.requirements}). |
| 9770 | + |
| 9771 | +\pnum |
| 9772 | +\effects Allocates memory for an object of type \tcode{T} |
| 9773 | +(or \tcode{U[N]} when \tcode{T} is \tcode{U[]}, |
| 9774 | +where \tcode{N} is determined from \placeholder{args} as specified by the concrete overload). |
| 9775 | +The object is initialized from \placeholder{args} as specified by the concrete overload. |
| 9776 | +The \tcode{allocate_shared} templates use a copy of \tcode{a} |
| 9777 | +(rebound for an unspecified \tcode{value_type}) to allocate memory. |
| 9778 | +If an exception is thrown, the functions have no effect. |
| 9779 | + |
| 9780 | +\pnum |
| 9781 | +\returns A \tcode{shared_ptr} instance that stores and owns the address of |
| 9782 | +the newly constructed object. |
| 9783 | + |
| 9784 | +\pnum |
| 9785 | +\postconditions \tcode{r.get() != 0 \&\& r.use_count() == 1}, |
| 9786 | +where \tcode{r} is the return value. |
| 9787 | + |
| 9788 | +\pnum |
| 9789 | +\throws \tcode{bad_alloc}, or |
| 9790 | +an exception thrown from \tcode{allocate} or from the initialization of the object. |
| 9791 | + |
| 9792 | +\pnum |
| 9793 | +\remarks |
| 9794 | +\begin{itemize} |
| 9795 | +\item |
| 9796 | + Implementations should perform no more than one memory allocation. |
| 9797 | + \begin{note} |
| 9798 | + This provides efficiency equivalent to an intrusive smart pointer. |
| 9799 | + \end{note} |
| 9800 | +\item |
| 9801 | + When an object of an array type \tcode{U} is specified to have |
| 9802 | + an initial value of \tcode{u} (of the same type), |
| 9803 | + this shall be interpreted to mean that |
| 9804 | + each array element of the object has as its initial value |
| 9805 | + the corresponding element from \tcode{u}. |
| 9806 | +\item |
| 9807 | + When an object of an array type is specified to have |
| 9808 | + a default initial value, |
| 9809 | + this shall be interpreted to mean that each array element of the object |
| 9810 | + has a default initial value. |
| 9811 | +\item |
| 9812 | + When a (sub)object of a non-array type \tcode{U} is specified to have |
| 9813 | + an initial value of \tcode{v}, or \tcode{U(l...)}, |
| 9814 | + where \tcode{l...} is a list of constructor arguments, |
| 9815 | + \tcode{make_shared} shall initialize this (sub)object |
| 9816 | + via the expression \tcode{::new(pv) U(v)} or \tcode{::new(pv) U(l...)} respectively, |
| 9817 | + where \tcode{pv} has type \tcode{void*} and points to storage |
| 9818 | + suitable to hold an object of type \tcode{U}. |
| 9819 | +\item |
| 9820 | + When a (sub)object of a non-array type \tcode{U} is specified to have |
| 9821 | + an initial value of \tcode{v}, or \tcode{U(l...)}, |
| 9822 | + where \tcode{l...} is a list of constructor arguments, |
| 9823 | + \tcode{allocate_shared} shall initialize this (sub)object |
| 9824 | + via the expression |
| 9825 | + \begin{itemize} |
| 9826 | + \item \tcode{allocator_traits<A2>::construct(a2, pv, v)} or |
| 9827 | + \item \tcode{allocator_traits<A2>::construct(a2, pv, l...)} |
| 9828 | + \end{itemize} |
| 9829 | + respectively, |
| 9830 | + where \tcode{pv} points to storage |
| 9831 | + suitable to hold an object of type \tcode{U} and |
| 9832 | + \tcode{a2} of type \tcode{A2} is a rebound copy of |
| 9833 | + the allocator \tcode{a} passed to \tcode{allocate_shared} |
| 9834 | + such that its \tcode{value_type} is \tcode{U}. |
| 9835 | +\item |
| 9836 | + When a (sub)object of non-array type \tcode{U} is specified to have |
| 9837 | + a default initial value, |
| 9838 | + \tcode{make_shared} shall initialize this (sub)object |
| 9839 | + via the expression \tcode{::new(pv) U()}, |
| 9840 | + where \tcode{pv} has type \tcode{void*} and points to storage |
| 9841 | + suitable to hold an object of type \tcode{U}. |
| 9842 | +\item |
| 9843 | + When a (sub)object of non-array type \tcode{U} is specified to have |
| 9844 | + a default initial value, |
| 9845 | + \tcode{allocate_shared} shall initialize this (sub)object |
| 9846 | + via the expression \tcode{allocator_traits<A2>::construct(a2, pv)}, |
| 9847 | + where \tcode{pv} points to storage |
| 9848 | + suitable to hold an object of type \tcode{U} and |
| 9849 | + \tcode{a2} of type \tcode{A2} is a rebound copy of |
| 9850 | + the allocator \tcode{a} passed to \tcode{allocate_shared} |
| 9851 | + such that its \tcode{value_type} is \tcode{U}. |
| 9852 | +\item |
| 9853 | + Array elements are initialized in ascending order of their addresses. |
| 9854 | +\item |
| 9855 | + When the lifetime of the object managed by the return value ends, or |
| 9856 | + when the initialization of an array element throws an exception, |
| 9857 | + the initialized elements should be destroyed in the reverse order of their construction. |
| 9858 | +\end{itemize} |
| 9859 | +\begin{note} |
| 9860 | +These functions will typically allocate more memory than \tcode{sizeof(T)} to |
| 9861 | +allow for internal bookkeeping structures such as reference counts. |
| 9862 | +\end{note} |
| 9863 | +\end{itemdescr} |
| 9864 | + |
9733 | 9865 | \indexlibrary{\idxcode{make_shared}}%
|
9734 | 9866 | \indexlibrary{\idxcode{allocate_shared}}%
|
9735 | 9867 | \begin{itemdecl}
|
9736 | 9868 | template<class T, class... Args>
|
9737 |
| - shared_ptr<T> make_shared(Args&&... args); |
| 9869 | + shared_ptr<T> make_shared(Args&&... args); // \tcode{T} is not array |
9738 | 9870 | template<class T, class A, class... Args>
|
9739 |
| - shared_ptr<T> allocate_shared(const A& a, Args&&... args); |
| 9871 | + shared_ptr<T> allocate_shared(const A& a, Args&&... args); // \tcode{T} is not array |
9740 | 9872 | \end{itemdecl}
|
9741 | 9873 |
|
9742 | 9874 | \begin{itemdescr}
|
9743 | 9875 | \pnum
|
9744 |
| -\requires The expression \tcode{::new (pv) T(std::forward<Args>(args)...)}, |
9745 |
| -where \tcode{pv} has type \tcode{void*} and points to storage suitable |
9746 |
| -to hold an object of type \tcode{T}, shall be well formed. \tcode{A} shall |
9747 |
| -be an allocator~(\ref{allocator.requirements}). The copy constructor |
9748 |
| -and destructor of \tcode{A} shall not throw exceptions. |
| 9876 | +\returns A \tcode{shared_ptr} to an object of type \tcode{T} |
| 9877 | +with an initial value \tcode{T(forward<Args>(args)...)}. |
9749 | 9878 |
|
9750 | 9879 | \pnum
|
9751 |
| -\effects Allocates memory suitable for an object of type \tcode{T} |
9752 |
| -and constructs an object in that memory via the placement |
9753 |
| -\grammarterm{new-expression} |
9754 |
| -\tcode{::new (pv) T(std::forward<Args>(args)...)}. |
9755 |
| -The template \tcode{allocate_shared} uses a copy of \tcode{a} to |
9756 |
| -allocate memory. If an exception is thrown, the functions have no effect. |
| 9880 | +\remarks These overloads shall only participate in overload resolution |
| 9881 | +when \tcode{T} is not an array type. |
| 9882 | +The \tcode{shared_ptr} constructors called by these functions |
| 9883 | +enable \tcode{shared_from_this} |
| 9884 | +with the address of the newly constructed object of type \tcode{T}. |
9757 | 9885 |
|
9758 | 9886 | \pnum
|
9759 |
| -\returns A \tcode{shared_ptr} instance that stores and owns |
9760 |
| -the address of the newly constructed object of type \tcode{T}. |
| 9887 | +\begin{example} |
| 9888 | +\begin{codeblock} |
| 9889 | +shared_ptr<int> p = make_shared<int>(); // \tcode{shared_ptr} to \tcode{int()} |
| 9890 | +shared_ptr<vector<int>> q = make_shared<vector<int>>(16, 1); |
| 9891 | + // \tcode{shared_ptr} to vector of \tcode{16} elements with value \tcode{1} |
| 9892 | +\end{codeblock} |
| 9893 | +\end{example} |
| 9894 | +\end{itemdescr} |
9761 | 9895 |
|
| 9896 | +\indexlibrary{\idxcode{make_shared}}% |
| 9897 | +\indexlibrary{\idxcode{allocate_shared}}% |
| 9898 | +\begin{itemdecl} |
| 9899 | +template<class T> shared_ptr<T> make_shared(size_t N); // \tcode{T} is \tcode{U[]} |
| 9900 | +template<class T, class A> |
| 9901 | + shared_ptr<T> allocate_shared(const A& a, size_t N); // \tcode{T} is \tcode{U[]} |
| 9902 | +\end{itemdecl} |
| 9903 | + |
| 9904 | +\begin{itemdescr} |
9762 | 9905 | \pnum
|
9763 |
| -\postconditions \tcode{get() != 0 \&\& use_count() == 1}. |
| 9906 | +\returns A \tcode{shared_ptr} to an object of type \tcode{U[N]} |
| 9907 | +with a default initial value, |
| 9908 | +where \tcode{U} is \tcode{remove_extent_t<T>}. |
9764 | 9909 |
|
9765 | 9910 | \pnum
|
9766 |
| -\throws \tcode{bad_alloc}, or an exception thrown from |
9767 |
| -\tcode{A::allocate} or from the constructor of \tcode{T}. |
| 9911 | +\remarks These overloads shall only participate in overload resolution |
| 9912 | +when \tcode{T} is of the form \tcode{U[]}. |
9768 | 9913 |
|
9769 | 9914 | \pnum
|
9770 |
| -\remarks The \tcode{shared_ptr} constructor called by this function |
9771 |
| -enables \tcode{shared_from_this} with the address |
9772 |
| -of the newly constructed object of type \tcode{T}. |
9773 |
| -Implementations should |
9774 |
| -perform no more than one memory allocation. \begin{note} This provides |
9775 |
| -efficiency equivalent to an intrusive smart pointer. \end{note} |
| 9915 | +\begin{example} |
| 9916 | +\begin{codeblock} |
| 9917 | +shared_ptr<double[]> p = make_shared<double[]>(1024); |
| 9918 | + // \tcode{shared_ptr} to a value-initialized \tcode{double[1024]} |
| 9919 | +shared_ptr<double[][2][2]> q = make_shared<double[][2][2]>(6); |
| 9920 | + // \tcode{shared_ptr} to a value-initialized \tcode{double[6][2][2]} |
| 9921 | +\end{codeblock} |
| 9922 | +\end{example} |
| 9923 | +\end{itemdescr} |
| 9924 | + |
| 9925 | +\indexlibrary{\idxcode{make_shared}}% |
| 9926 | +\indexlibrary{\idxcode{allocate_shared}}% |
| 9927 | +\begin{itemdecl} |
| 9928 | +template<class T> shared_ptr<T> make_shared(); // \tcode{T} is \tcode{U[N]} |
| 9929 | +template<class T, class A> |
| 9930 | + shared_ptr<T> allocate_shared(const A& a); // \tcode{T} is \tcode{U[N]} |
| 9931 | +\end{itemdecl} |
| 9932 | + |
| 9933 | +\begin{itemdescr} |
| 9934 | +\pnum |
| 9935 | +\returns A \tcode{shared_ptr} to an object of type \tcode{T} |
| 9936 | +with a default initial value. |
| 9937 | + |
| 9938 | +\pnum |
| 9939 | +\remarks These overloads shall only participate in overload resolution |
| 9940 | +when \tcode{T} is of the form \tcode{U[N]}. |
9776 | 9941 |
|
9777 | 9942 | \pnum
|
9778 |
| -\begin{note} These functions will typically allocate more memory |
9779 |
| -than \tcode{sizeof(T)} to allow for internal bookkeeping structures such |
9780 |
| -as the reference counts. \end{note} |
| 9943 | +\begin{example} |
| 9944 | +\begin{codeblock} |
| 9945 | +shared_ptr<double[1024]> p = make_shared<double[1024]>(); |
| 9946 | + // \tcode{shared_ptr} to a value-initialized \tcode{double[1024]} |
| 9947 | +shared_ptr<double[6][2][2]> q = make_shared<double[6][2][2]>(); |
| 9948 | + // \tcode{shared_ptr} to a value-initialized \tcode{double[6][2][2]} |
| 9949 | +\end{codeblock} |
| 9950 | +\end{example} |
| 9951 | +\end{itemdescr} |
| 9952 | + |
| 9953 | +\indexlibrary{\idxcode{make_shared}}% |
| 9954 | +\indexlibrary{\idxcode{allocate_shared}}% |
| 9955 | +\begin{itemdecl} |
| 9956 | +template<class T> |
| 9957 | + shared_ptr<T> make_shared(size_t N, const remove_extent_t<T>& u); // \tcode{T} is \tcode{U[]} |
| 9958 | +template<class T, class A> |
| 9959 | + shared_ptr<T> allocate_shared(const A& a, size_t N, |
| 9960 | + const remove_extent_t<T>& u); // \tcode{T} is \tcode{U[]} |
| 9961 | +\end{itemdecl} |
| 9962 | + |
| 9963 | +\begin{itemdescr} |
| 9964 | +\pnum |
| 9965 | +\returns A \tcode{shared_ptr} to an object of type \tcode{U[N]}, |
| 9966 | +where \tcode{U} is \tcode{remove_extent_t<T>} and |
| 9967 | +each array element has an initial value of \tcode{u}. |
| 9968 | + |
| 9969 | +\pnum |
| 9970 | +\remarks These overloads shall only participate in overload resolution |
| 9971 | +when \tcode{T} is of the form \tcode{U[]}. |
| 9972 | + |
| 9973 | +\pnum |
| 9974 | +\begin{example} |
| 9975 | +\begin{codeblock} |
| 9976 | +shared_ptr<double[]> p = make_shared<double[]>(1024, 1.0); |
| 9977 | + // \tcode{shared_ptr} to a \tcode{double[1024]}, where each element is \tcode{1.0} |
| 9978 | +shared_ptr<double[][2]> q = make_shared<double[][2]>(6, {1.0, 0.0}); |
| 9979 | + // \tcode{shared_ptr} to a \tcode{double[6][2]}, where each \tcode{double[2]} element is \tcode{\{1.0, 0.0\}} |
| 9980 | +shared_ptr<vector<int>[]> r = make_shared<vector<int>[]>(4, {1, 2}); |
| 9981 | + // \tcode{shared_ptr} to a \tcode{vector<int>[4]}, where each vector has contents \tcode{\{1, 2\}} |
| 9982 | +\end{codeblock} |
| 9983 | +\end{example} |
| 9984 | +\end{itemdescr} |
| 9985 | + |
| 9986 | +\indexlibrary{\idxcode{make_shared}}% |
| 9987 | +\indexlibrary{\idxcode{allocate_shared}}% |
| 9988 | +\begin{itemdecl} |
| 9989 | +template<class T> |
| 9990 | + shared_ptr<T> make_shared(const remove_extent_t<T>& u); // \tcode{T} is \tcode{U[N]} |
| 9991 | +template<class T, class A> |
| 9992 | + shared_ptr<T> allocate_shared(const A& a, |
| 9993 | + const remove_extent_t<T>& u); // \tcode{T} is \tcode{U[N]} |
| 9994 | +\end{itemdecl} |
| 9995 | + |
| 9996 | +\begin{itemdescr} |
| 9997 | +\pnum |
| 9998 | +\returns A \tcode{shared_ptr} to an object of type \tcode{T}, |
| 9999 | +where each array element of type \tcode{remove_extent_t<T>} |
| 10000 | +has an initial value of \tcode{u}. |
| 10001 | + |
| 10002 | +\pnum |
| 10003 | +\remarks These overloads shall only participate in overload resolution |
| 10004 | +when \tcode{T} is of the form \tcode{U[N]}. |
| 10005 | + |
| 10006 | +\pnum |
| 10007 | +\begin{example} |
| 10008 | +\begin{codeblock} |
| 10009 | +shared_ptr<double[1024]> p = make_shared<double[1024]>(1.0); |
| 10010 | + // \tcode{shared_ptr} to a \tcode{double[1024]}, where each element is \tcode{1.0} |
| 10011 | +shared_ptr<double[6][2]> q = make_shared<double[6][2]>({1.0, 0.0}); |
| 10012 | + // \tcode{shared_ptr} to a \tcode{double[6][2]}, where each double[2] element is \tcode{\{1.0, 0.0\}} |
| 10013 | +shared_ptr<vector<int>[4]> r = make_shared<vector<int>[4]>({1, 2}); |
| 10014 | + // \tcode{shared_ptr} to a \tcode{vector<int>[4]}, where each vector has contents \tcode{\{1, 2\}} |
| 10015 | +\end{codeblock} |
| 10016 | +\end{example} |
9781 | 10017 | \end{itemdescr}
|
9782 | 10018 |
|
9783 | 10019 | \rSec4[util.smartptr.shared.cmp]{\tcode{shared_ptr} comparison}
|
|
0 commit comments