forked from boostorg/smart_ptr
Add fourth form of make_unique for objects
To support initialization syntax that Args&&... cannot forward perfectly.
This commit is contained in:
@ -11,6 +11,7 @@
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/smart_ptr/detail/up_if_not_array.hpp>
|
||||
#include <utility>
|
||||
|
||||
namespace boost {
|
||||
template<typename T>
|
||||
@ -27,6 +28,12 @@ namespace boost {
|
||||
}
|
||||
#endif
|
||||
|
||||
template<typename T>
|
||||
inline typename boost::detail::up_if_not_array<T>::type
|
||||
make_unique(T&& value) {
|
||||
return std::unique_ptr<T>(new T(std::move(value)));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename boost::detail::up_if_not_array<T>::type
|
||||
make_unique_noinit() {
|
||||
|
@ -26,6 +26,9 @@
|
||||
unique_ptr<U> <a href="#functions">make_unique</a>(Args&&... args);
|
||||
#endif
|
||||
|
||||
template<typename U> // U is not array
|
||||
unique_ptr<U> <a href="#functions">make_unique</a>(U&& value);
|
||||
|
||||
template<typename U> // U is not array
|
||||
unique_ptr<U> <a href="#functions">make_unique_noinit</a>();
|
||||
|
||||
@ -59,6 +62,14 @@ unique_ptr<U> make_unique(Args&&... args);</pre>
|
||||
expression <code>new U(forward<Args>(args)...)</code>.</p>
|
||||
</blockquote>
|
||||
<pre>template<typename U> // U is not array
|
||||
unique_ptr<U> make_unique(U&& value);</pre>
|
||||
<blockquote>
|
||||
<p><b>Requires:</b> The expression <code>new U(move(value))</code> shall
|
||||
be well-formed.</p>
|
||||
<p><b>Effects:</b> Constructs an object of type <code>U</code> via the
|
||||
expression <code>new U(move(value))</code>.</p>
|
||||
</blockquote>
|
||||
<pre>template<typename U> // U is not array
|
||||
unique_ptr<U> make_unique_noinit();</pre>
|
||||
<blockquote>
|
||||
<p><b>Requires:</b> The expression <code>new U</code> shall be
|
||||
@ -87,28 +98,33 @@ unique_ptr<U> make_unique_noinit(size_t size);</pre>
|
||||
<h2><a name="example">Examples</a></h2>
|
||||
<p>For objects with value-initialization.</p>
|
||||
<blockquote>
|
||||
<pre>std::unique_ptr<float> p1 = boost::make_unique<float>();
|
||||
std::unique_ptr<point> p2 = boost::make_unique<point>();</pre>
|
||||
<pre>unique_ptr<float> p1 = boost::make_unique<float>();
|
||||
unique_ptr<point> p2 = boost::make_unique<point>();</pre>
|
||||
</blockquote>
|
||||
<p>For objects with construction arguments.</p>
|
||||
<blockquote>
|
||||
<pre>std::unique_ptr<float> p3 = boost::make_unique<float>(1.5);
|
||||
std::unique_ptr<point> p4 = boost::make_unique<point>(x, y);</pre>
|
||||
<pre>unique_ptr<float> p3 = boost::make_unique<float>(1.0f);
|
||||
unique_ptr<point> p4 = boost::make_unique<point>(x, y);</pre>
|
||||
</blockquote>
|
||||
<p>For objects with given value.</p>
|
||||
<blockquote>
|
||||
<pre>unique_ptr<string> p4 = boost::make_unique<string>({'a', 'b'});
|
||||
unique_ptr<type> p5 = boost::make_unique<type>({3, 5, 4});</pre>
|
||||
</blockquote>
|
||||
<p>For objects with default-initialization.</p>
|
||||
<blockquote>
|
||||
<pre>std::unique_ptr<float> p4 = boost::make_unique_noinit<float>();
|
||||
std::unique_ptr<point> p5 = boost::make_unique_noinit<point>();</pre>
|
||||
<pre>unique_ptr<float> p6 = boost::make_unique_noinit<float>();
|
||||
unique_ptr<point> p7 = boost::make_unique_noinit<point>();</pre>
|
||||
</blockquote>
|
||||
<p>For arrays with value-initialization.</p>
|
||||
<blockquote>
|
||||
<pre>std::unique_ptr<double[]> a1 = boost::make_unique<double[]>();
|
||||
std::unique_ptr<int[][4]> a2 = boost::make_unique<int[][4]>();</pre>
|
||||
<pre>unique_ptr<double[]> a1 = boost::make_unique<double[]>();
|
||||
unique_ptr<int[][4]> a2 = boost::make_unique<int[][4]>();</pre>
|
||||
</blockquote>
|
||||
<p>For arrays with default-initialization.</p>
|
||||
<blockquote>
|
||||
<pre>std::unique_ptr<double[]> a3 = boost::make_unique_noinit<double[]>();
|
||||
std::unique_ptr<int[][4]> a4 = boost::make_unique_noinit<int[][4]>();</pre>
|
||||
<pre>unique_ptr<double[]> a3 = boost::make_unique_noinit<double[]>();
|
||||
unique_ptr<int[][4]> a4 = boost::make_unique_noinit<int[][4]>();</pre>
|
||||
</blockquote>
|
||||
<h2><a name="history">History</a></h2>
|
||||
<p>January 2014. Glen Fernandes contributed implementations of
|
||||
|
@ -151,6 +151,7 @@ import testing ;
|
||||
|
||||
[ run make_unique_test.cpp ]
|
||||
[ run make_unique_args_test.cpp ]
|
||||
[ run make_unique_value_test.cpp ]
|
||||
[ run make_unique_noinit_test.cpp ]
|
||||
[ run make_unique_throws_test.cpp ]
|
||||
[ run make_unique_array_test.cpp ]
|
||||
|
@ -53,7 +53,7 @@ int main() {
|
||||
|
||||
#if !defined( BOOST_NO_CXX11_VARIADIC_TEMPLATES )
|
||||
{
|
||||
std::unique_ptr<const type> a1 = boost::make_unique<type>(1);
|
||||
std::unique_ptr<type> a1 = boost::make_unique<type>(1);
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
BOOST_TEST(type::instances == 1);
|
||||
BOOST_TEST(a1->sum == 1);
|
||||
@ -62,7 +62,7 @@ int main() {
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<const type> a1 = boost::make_unique<type>(1, 2);
|
||||
std::unique_ptr<type> a1 = boost::make_unique<type>(1, 2);
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
BOOST_TEST(type::instances == 1);
|
||||
BOOST_TEST(a1->sum == 1 + 2);
|
||||
@ -71,7 +71,7 @@ int main() {
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<const type> a1 = boost::make_unique<type>(1, 2, 3);
|
||||
std::unique_ptr<type> a1 = boost::make_unique<type>(1, 2, 3);
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
BOOST_TEST(type::instances == 1);
|
||||
BOOST_TEST(a1->sum == 1 + 2 + 3);
|
||||
@ -80,7 +80,7 @@ int main() {
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<const type> a1 = boost::make_unique<type>(1, 2, 3, 4);
|
||||
std::unique_ptr<type> a1 = boost::make_unique<type>(1, 2, 3, 4);
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
BOOST_TEST(type::instances == 1);
|
||||
BOOST_TEST(a1->sum == 1 + 2 + 3 + 4);
|
||||
@ -89,7 +89,7 @@ int main() {
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<const type> a1 = boost::make_unique<type>(1, 2, 3, 4, 5);
|
||||
std::unique_ptr<type> a1 = boost::make_unique<type>(1, 2, 3, 4, 5);
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
BOOST_TEST(type::instances == 1);
|
||||
BOOST_TEST(a1->sum == 1 + 2 + 3 + 4 + 5);
|
||||
@ -98,7 +98,7 @@ int main() {
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<const type> a1 = boost::make_unique<type>(1, 2, 3, 4, 5, 6);
|
||||
std::unique_ptr<type> a1 = boost::make_unique<type>(1, 2, 3, 4, 5, 6);
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
BOOST_TEST(type::instances == 1);
|
||||
BOOST_TEST(a1->sum == 1 + 2 + 3 + 4 + 5 + 6);
|
||||
@ -107,7 +107,7 @@ int main() {
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<const type> a1 = boost::make_unique<type>(1, 2, 3, 4, 5, 6, 7);
|
||||
std::unique_ptr<type> a1 = boost::make_unique<type>(1, 2, 3, 4, 5, 6, 7);
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
BOOST_TEST(type::instances == 1);
|
||||
BOOST_TEST(a1->sum == 1 + 2 + 3 + 4 + 5 + 6 + 7);
|
||||
@ -116,7 +116,7 @@ int main() {
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<const type> a1 = boost::make_unique<type>(1, 2, 3, 4, 5, 6, 7, 8);
|
||||
std::unique_ptr<type> a1 = boost::make_unique<type>(1, 2, 3, 4, 5, 6, 7, 8);
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
BOOST_TEST(type::instances == 1);
|
||||
BOOST_TEST(a1->sum == 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8);
|
||||
@ -125,7 +125,7 @@ int main() {
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<const type> a1 = boost::make_unique<type>(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
std::unique_ptr<type> a1 = boost::make_unique<type>(1, 2, 3, 4, 5, 6, 7, 8, 9);
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
BOOST_TEST(type::instances == 1);
|
||||
BOOST_TEST(a1->sum == 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9);
|
||||
|
@ -40,17 +40,7 @@ int main() {
|
||||
std::unique_ptr<int[][2]> a1 = boost::make_unique_noinit<int[][2]>(2);
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<const int[]> a1 = boost::make_unique_noinit<const int[]>(3);
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<const int[][2]> a1 = boost::make_unique_noinit<const int[][2]>(2);
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
}
|
||||
|
||||
|
||||
BOOST_TEST(type::instances == 0);
|
||||
{
|
||||
std::unique_ptr<type[]> a1 = boost::make_unique_noinit<type[]>(3);
|
||||
@ -72,7 +62,6 @@ int main() {
|
||||
BOOST_TEST(type::instances == 0);
|
||||
{
|
||||
std::unique_ptr<const type[]> a1 = boost::make_unique_noinit<const type[]>(3);
|
||||
const type* a2 = a1.get();
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
BOOST_TEST(type::instances == 3);
|
||||
a1.reset();
|
||||
|
@ -35,11 +35,6 @@ int main() {
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<const int> a1 = boost::make_unique_noinit<const int>();
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
}
|
||||
|
||||
BOOST_TEST(type::instances == 0);
|
||||
{
|
||||
std::unique_ptr<type> a1 = boost::make_unique_noinit<type>();
|
||||
@ -52,7 +47,6 @@ int main() {
|
||||
BOOST_TEST(type::instances == 0);
|
||||
{
|
||||
std::unique_ptr<const type> a1 = boost::make_unique_noinit<const type>();
|
||||
const type* a2 = a1.get();
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
BOOST_TEST(type::instances == 1);
|
||||
a1.reset();
|
||||
|
51
test/make_unique_value_test.cpp
Normal file
51
test/make_unique_value_test.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2014 Glen Joseph Fernandes
|
||||
* glenfe at live dot com
|
||||
*
|
||||
* Distributed under the Boost Software License,
|
||||
* Version 1.0. (See accompanying file LICENSE_1_0.txt
|
||||
* or copy at http://boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
#include <boost/smart_ptr/make_unique_object.hpp>
|
||||
|
||||
struct type {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
int main() {
|
||||
#if !defined(BOOST_NO_CXX11_SMART_PTR)
|
||||
{
|
||||
std::unique_ptr<type> a1 = boost::make_unique<type>();
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
BOOST_TEST(a1->x == 0);
|
||||
BOOST_TEST(a1->y == 0);
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<const type> a1 = boost::make_unique<const type>();
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
BOOST_TEST(a1->x == 0);
|
||||
BOOST_TEST(a1->y == 0);
|
||||
}
|
||||
|
||||
#if !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
|
||||
{
|
||||
std::unique_ptr<type> a1 = boost::make_unique<type>({ 1, 2 });
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
BOOST_TEST(a1->x == 1);
|
||||
BOOST_TEST(a1->y == 2);
|
||||
}
|
||||
|
||||
{
|
||||
std::unique_ptr<const type> a1 = boost::make_unique<const type>({ 1, 2 });
|
||||
BOOST_TEST(a1.get() != 0);
|
||||
BOOST_TEST(a1->x == 1);
|
||||
BOOST_TEST(a1->y == 2);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user