boost.png (6897 bytes)make_unique

Introduction
Synopsis
Free Functions
Examples
History

Introduction

The header file <boost/make_unique.hpp> provides 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(U&& value);

    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(U&& value);

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

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

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 T[size]() shall be well-formed.

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

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

Requires: The expression new T[size] shall be well-formed.

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

Examples

For objects with value-initialization.

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

For objects with construction arguments.

unique_ptr<float> p3 = boost::make_unique<float>(1.0f);
unique_ptr<point> p4 = boost::make_unique<point>(x, y);

For objects with given value.

unique_ptr<string> p4 = boost::make_unique<string>({'a', 'b'});
unique_ptr<type>   p5 = boost::make_unique<type>({3, 5, 4});

For objects with default-initialization.

unique_ptr<float> p6 = boost::make_unique_noinit<float>();
unique_ptr<point> p7 = boost::make_unique_noinit<point>();

For arrays with value-initialization.

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

For arrays with default-initialization.

unique_ptr<double[]> a3 = boost::make_unique_noinit<double[]>();
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.