GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::make_unique,std::make_unique_for_overwrite(3) C++ Standard Libary std::make_unique,std::make_unique_for_overwrite(3)

std::make_unique,std::make_unique_for_overwrite - std::make_unique,std::make_unique_for_overwrite


Defined in header <memory>
(since
C++14)
template< class T, class... Args > (until
unique_ptr<T> make_unique( Args&&... C++23)
args ); (only for
non-array
types)
(since
template< class T, class... Args > C++23)
constexpr unique_ptr<T> make_unique( (only for
Args&&... args ); non-array
types)
(since
C++14)
(until
template< class T > C++23)
unique_ptr<T> make_unique( (only for
std::size_t size ); array
types
with
unknown
bound)
(since
C++23)
template< class T > (only for
constexpr unique_ptr<T> make_unique( array
std::size_t size ); types
with
unknown
bound)
(since
C++14)
template< class T, class... Args > (only for
/* unspecified */ make_unique( (3) array
Args&&... args ) = delete; types
with
known
(1) bound)
(since
C++20)
template< class T > (until
unique_ptr<T> C++23)
make_unique_for_overwrite( ); (only for
(2) non-array
types)
(since
template< class T > C++23)
constexpr unique_ptr<T> (only for
make_unique_for_overwrite( ); non-array
types)
(since
C++20)
(until
template< class T > C++23)
unique_ptr<T> (only for
make_unique_for_overwrite( array
std::size_t size ); (4) types
with
unknown
bound)
(since
C++23)
template< class T > (5) (only for
constexpr unique_ptr<T> array
make_unique_for_overwrite( types
std::size_t size ); with
unknown
bound)
(since
C++20)
template< class T, class... Args > (only for
/* unspecified */ (6) array
make_unique_for_overwrite( Args&&... types
args ) = delete; with
known
bound)


Constructs an object of type T and wraps it in a std::unique_ptr.


1) Constructs a non-array type T. The arguments args are passed to the constructor
of T. This overload participates in overload resolution only if T is not an array
type. The function is equivalent to:


unique_ptr<T>(new T(std::forward<Args>(args)...))


2) Constructs an array of the given dynamic size. The array elements are
value-initialized. This overload participates in overload resolution only if T is an
array of unknown bound. The function is equivalent to:


unique_ptr<T>(new std::remove_extent_t<T>[size]())


3,6) Construction of arrays of known bound is disallowed.
4) Same as (1), except that the object is default-initialized. This overload
participates in overload resolution only if T is not an array type. The function is
equivalent to:


unique_ptr<T>(new T)


5) Same as (2), except that the array is default-initialized. This overload
participates in overload resolution only if T is an array of unknown bound. The
function is equivalent to:


unique_ptr<T>(new std::remove_extent_t<T>[size])


args - list of arguments with which an instance of T will be constructed.
size - the length of the array to construct


std::unique_ptr of an instance of type T.


May throw std::bad_alloc or any exception thrown by the constructor of T. If an
exception is thrown, this function has no effect.


Possible Implementation


// C++14 make_unique
namespace detail {
template<class>
constexpr bool is_unbounded_array_v = false;
template<class T>
constexpr bool is_unbounded_array_v<T[]> = true;


template<class>
constexpr bool is_bounded_array_v = false;
template<class T, std::size_t N>
constexpr bool is_bounded_array_v<T[N]> = true;
} // namespace detail


template<class T, class... Args>
std::enable_if_t<!std::is_array<T>::value, std::unique_ptr<T>>
make_unique(Args&&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}


template<class T>
std::enable_if_t<detail::is_unbounded_array_v<T>, std::unique_ptr<T>>
make_unique(std::size_t n)
{
return std::unique_ptr<T>(new std::remove_extent_t<T>[n]());
}


template<class T, class... Args>
std::enable_if_t<detail::is_bounded_array_v<T>> make_unique(Args&&...) = delete;


// C++20 make_unique_for_overwrite
template<class T>
requires !std::is_array_v<T>
std::unique_ptr<T> make_unique_for_overwrite()
{
return std::unique_ptr<T>(new T);
}


template<class T>
requires std::is_unbounded_array_v<T>
std::unique_ptr<T> make_unique_for_overwrite(std::size_t n)
{
return std::unique_ptr<T>(new std::remove_extent_t<T>[n]);
}


template<class T, class... Args>
requires std::is_bounded_array_v<T>
void make_unique_for_overwrite(Args&&...) = delete;


Unlike std::make_shared (which has std::allocate_shared), std::make_unique does not
have an allocator-aware counterpart. allocate_unique proposed in P0211 would be
required to invent the deleter type D for the std::unique_ptr<T,D> it returns which
would contain an allocator object and invoke both destroy and deallocate in its
operator().


Feature-test macro: __cpp_lib_make_unique


Feature-test macro: __cpp_lib_smart_ptr_for_overwrite (for overloads (4-6))


Feature-test macro: __cpp_lib_constexpr_memory ((C++23) constexpr for overloads
(1,2,4,5))


This section is incomplete
Reason: add make_unique_for_overwrite() demo

// Run this code


#include <iostream>
#include <iomanip>
#include <memory>


struct Vec3
{
int x, y, z;


// following constructor is no longer needed since C++20
Vec3(int x = 0, int y = 0, int z = 0) noexcept : x(x), y(y), z(z) { }


friend std::ostream& operator<<(std::ostream& os, const Vec3& v) {
return os << "{ x=" << v.x << ", y=" << v.y << ", z=" << v.z << " }";
}
};


int main()
{
// Use the default constructor.
std::unique_ptr<Vec3> v1 = std::make_unique<Vec3>();
// Use the constructor that matches these arguments
std::unique_ptr<Vec3> v2 = std::make_unique<Vec3>(0,1,2);
// Create a unique_ptr to an array of 5 elements
std::unique_ptr<Vec3[]> v3 = std::make_unique<Vec3[]>(5);


std::cout << "make_unique<Vec3>(): " << *v1 << '\n'
<< "make_unique<Vec3>(0,1,2): " << *v2 << '\n'
<< "make_unique<Vec3[]>(5): ";
for (int i = 0; i < 5; i++) {
std::cout << std::setw(i ? 30 : 0) << v3[i] << '\n';
}
}


make_unique<Vec3>(): { x=0, y=0, z=0 }
make_unique<Vec3>(0,1,2): { x=0, y=1, z=2 }
make_unique<Vec3[]>(5): { x=0, y=0, z=0 }
{ x=0, y=0, z=0 }
{ x=0, y=0, z=0 }
{ x=0, y=0, z=0 }
{ x=0, y=0, z=0 }


constructor constructs a new unique_ptr
(public member function)
make_shared creates a shared pointer that manages a new object
make_shared_for_overwrite (function template)
(C++20)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.