From 7fef3bb40b10badd8f6783b8e38b288e086c074c Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Wed, 29 Jan 2014 07:25:30 -0800 Subject: [PATCH] Add top-level make_unique.hpp and documentation --- include/boost/make_unique.hpp | 14 ++++ make_unique.html | 124 ++++++++++++++++++++++++++++++++++ smart_ptr.htm | 8 +++ 3 files changed, 146 insertions(+) create mode 100644 include/boost/make_unique.hpp create mode 100644 make_unique.html diff --git a/include/boost/make_unique.hpp b/include/boost/make_unique.hpp new file mode 100644 index 0000000..c163673 --- /dev/null +++ b/include/boost/make_unique.hpp @@ -0,0 +1,14 @@ +/* + * 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) + */ +#ifndef BOOST_MAKE_UNIQUE_HPP_INCLUDED +#define BOOST_MAKE_UNIQUE_HPP_INCLUDED + +#include + +#endif diff --git a/make_unique.html b/make_unique.html new file mode 100644 index 0000000..b6d7a7d --- /dev/null +++ b/make_unique.html @@ -0,0 +1,124 @@ + + + + make_unique + + + +

boost.png (6897 bytes)make_unique

+

Introduction
+ Synopsis
+ Free Functions
+ Examples
+ History

+

Introduction

+

The header file <boost/make_unique.hpp> provides a family of + overloaded function template make_unique for convenient + creation of unique_ptr objects.

+

Synopsis

+
namespace boost {
+    template<typename U> // U is not array
+    unique_ptr<U> make_unique();
+
+#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
+    template<typename U, typename... Args> // U is not array
+    unique_ptr<U> make_unique(Args&&... args);
+#endif
+
+    template<typename U> // U is not array
+    unique_ptr<U> make_unique_noinit();
+
+    template<typename U> // U is T[]
+    unique_ptr<U> make_unique(size_t size);
+
+    template<typename U> // U is T[]
+    unique_ptr<U> make_unique_noinit(size_t size);
+}
+

Free Functions

+
template<typename U> // U is not array
+unique_ptr<U> make_unique();
+
+

Requires: The expression new U() shall be + well-formed.

+

Effects: Constructs an object of type U via the + expression new U().

+

Returns: A unique_ptr instance that stores and + owns the address of the newly constructed object.

+

Postconditions: get() != 0.

+

Throws: bad_alloc, or an exception thrown from + the constructor of U.

+
+
template<typename U, typename... Args> // U is not array
+unique_ptr<U> make_unique(Args&&... args);
+
+

Requires: The expression + new U(forward<Args>(args)...) shall be + well-formed.

+

Effects: Constructs an object of type U via the + expression new U(forward<Args>(args)...).

+
+
template<typename U> // U is not array
+unique_ptr<U> make_unique_noinit();
+
+

Requires: The expression new U shall be + well-formed.

+

Effects: Constructs an object of type U via the + expression new U.

+
+
template<typename U> // U is T[]
+unique_ptr<U> make_unique(size_t size);
+
+

Requires: The expression new U() shall be + well-formed.

+

Effects: Constructs an array of objects of type + U and size size via the expression + new U[size]().

+
+
template<typename U> // U is T[]
+unique_ptr<U> make_unique_noinit(size_t size);
+
+

Requires: The expression new U shall be + well-formed.

+

Effects: Constructs an array of objects of type + U and size size via the expression + new U[size].

+
+

Examples

+

For objects with value-initialization.

+
+
std::unique_ptr<float> p1 = boost::make_unique<float>();
+std::unique_ptr<point> p2 = boost::make_unique<point>();
+
+

For objects with construction arguments.

+
+
std::unique_ptr<float> p3 = boost::make_unique<float>(1.5);
+std::unique_ptr<point> p4 = boost::make_unique<point>(x, y);
+
+

For objects with default-initialization.

+
+
std::unique_ptr<float> p4 = boost::make_unique_noinit<float>();
+std::unique_ptr<point> p5 = boost::make_unique_noinit<point>();
+
+

For arrays with value-initialization.

+
+
std::unique_ptr<double[]> a1 = boost::make_unique<double[]>();
+std::unique_ptr<int[][4]> a2 = boost::make_unique<int[][4]>();
+
+

For arrays with default-initialization.

+
+
std::unique_ptr<double[]> a3 = boost::make_unique_noinit<double[]>();
+std::unique_ptr<int[][4]> a4 = boost::make_unique_noinit<int[][4]>();
+
+

History

+

January 2014. Glen Fernandes contributed implementations of + make_unique for objects and arrays.

+
+

$Date: 2014-01-20 11:10:00 -0800 (Mon, 20 Jan 2014) $

+

Copyright 2012-2014 Glen Fernandes. Distributed under the + Boost Software License, Version 1.0. See accompanying file + LICENSE_1_0.txt or copy at + + http://www.boost.org/LICENSE_1_0.txt.

+ + diff --git a/smart_ptr.htm b/smart_ptr.htm index 5fcfbb3..c16dceb 100644 --- a/smart_ptr.htm +++ b/smart_ptr.htm @@ -75,6 +75,11 @@ <boost/make_shared.hpp> Efficient creation of shared_ptr arrays. + + make_unique + <boost/make_unique.hpp> + Creation of unique_ptr objects and arrays. +

A test program, smart_ptr_test.cpp, is @@ -131,6 +136,9 @@

Functions which destroy objects of the pointed to type are prohibited from throwing exceptions by the common requirements.

History and Acknowledgements

+

January 2014. Glen Fernandes confined the overloads of make_shared + and allocate_shared for arrays to the specification in N3870 and + implemented make_unique for arrays and objects.

November 2012. Glen Fernandes provided implementations of make_shared and allocate_shared for arrays. They achieve a single allocation for an array that can be initialized with constructor arguments or initializer lists