Add fourth form of make_unique for objects

To support initialization syntax that Args&&... cannot forward perfectly.
This commit is contained in:
Glen Fernandes
2014-01-29 16:33:32 -08:00
parent f91e7e9ce7
commit 72e5fb6fd7
7 changed files with 95 additions and 37 deletions

View File

@ -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() {

View File

@ -26,6 +26,9 @@
unique_ptr&lt;U&gt; <a href="#functions">make_unique</a>(Args&amp;&amp;... args);
#endif
template&lt;typename U&gt; // U is not array
unique_ptr&lt;U&gt; <a href="#functions">make_unique</a>(U&amp;&amp; value);
template&lt;typename U&gt; // U is not array
unique_ptr&lt;U&gt; <a href="#functions">make_unique_noinit</a>();
@ -59,6 +62,14 @@ unique_ptr&lt;U&gt; make_unique(Args&amp;&amp;... args);</pre>
expression <code>new U(forward&lt;Args&gt;(args)...)</code>.</p>
</blockquote>
<pre>template&lt;typename U&gt; // U is not array
unique_ptr&lt;U&gt; make_unique(U&amp;&amp; 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&lt;typename U&gt; // U is not array
unique_ptr&lt;U&gt; make_unique_noinit();</pre>
<blockquote>
<p><b>Requires:</b> The expression <code>new U</code> shall be
@ -87,28 +98,33 @@ unique_ptr&lt;U&gt; 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&lt;float&gt; p1 = boost::make_unique&lt;float&gt;();
std::unique_ptr&lt;point&gt; p2 = boost::make_unique&lt;point&gt;();</pre>
<pre>unique_ptr&lt;float&gt; p1 = boost::make_unique&lt;float&gt;();
unique_ptr&lt;point&gt; p2 = boost::make_unique&lt;point&gt;();</pre>
</blockquote>
<p>For objects with construction arguments.</p>
<blockquote>
<pre>std::unique_ptr&lt;float&gt; p3 = boost::make_unique&lt;float&gt;(1.5);
std::unique_ptr&lt;point&gt; p4 = boost::make_unique&lt;point&gt;(x, y);</pre>
<pre>unique_ptr&lt;float&gt; p3 = boost::make_unique&lt;float&gt;(1.0f);
unique_ptr&lt;point&gt; p4 = boost::make_unique&lt;point&gt;(x, y);</pre>
</blockquote>
<p>For objects with given value.</p>
<blockquote>
<pre>unique_ptr&lt;string&gt; p4 = boost::make_unique&lt;string&gt;({'a', 'b'});
unique_ptr&lt;type&gt; p5 = boost::make_unique&lt;type&gt;({3, 5, 4});</pre>
</blockquote>
<p>For objects with default-initialization.</p>
<blockquote>
<pre>std::unique_ptr&lt;float&gt; p4 = boost::make_unique_noinit&lt;float&gt;();
std::unique_ptr&lt;point&gt; p5 = boost::make_unique_noinit&lt;point&gt;();</pre>
<pre>unique_ptr&lt;float&gt; p6 = boost::make_unique_noinit&lt;float&gt;();
unique_ptr&lt;point&gt; p7 = boost::make_unique_noinit&lt;point&gt;();</pre>
</blockquote>
<p>For arrays with value-initialization.</p>
<blockquote>
<pre>std::unique_ptr&lt;double[]&gt; a1 = boost::make_unique&lt;double[]&gt;();
std::unique_ptr&lt;int[][4]&gt; a2 = boost::make_unique&lt;int[][4]&gt;();</pre>
<pre>unique_ptr&lt;double[]&gt; a1 = boost::make_unique&lt;double[]&gt;();
unique_ptr&lt;int[][4]&gt; a2 = boost::make_unique&lt;int[][4]&gt;();</pre>
</blockquote>
<p>For arrays with default-initialization.</p>
<blockquote>
<pre>std::unique_ptr&lt;double[]&gt; a3 = boost::make_unique_noinit&lt;double[]&gt;();
std::unique_ptr&lt;int[][4]&gt; a4 = boost::make_unique_noinit&lt;int[][4]&gt;();</pre>
<pre>unique_ptr&lt;double[]&gt; a3 = boost::make_unique_noinit&lt;double[]&gt;();
unique_ptr&lt;int[][4]&gt; a4 = boost::make_unique_noinit&lt;int[][4]&gt;();</pre>
</blockquote>
<h2><a name="history">History</a></h2>
<p>January 2014. Glen Fernandes contributed implementations of

View File

@ -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 ]

View File

@ -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);

View File

@ -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();

View File

@ -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();

View 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();
}