跳到主要内容

std::make_unique, std::make_unique_for_overwrite

声明

// 3)
template< class T, class... Args >
/* unspecified */ make_unique( Args&&... args ) = delete;
// 6)
template< class T, class... Args >
/* unspecified */ make_unique_for_overwrite( Args&&... args ) = delete;

构造一个类型为T的对象,并将其包装在std::unique_ptr中。


构造一个非数组类型T。参数args将被传递给T的构造函数。此重载仅当T不是数组类型时才参与重载解析。该函数等同于

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

构造一个指定动态大小的数组。数组元素被值初始化。此重载仅当T是未知界数组时才参与重载解析。该函数等同于

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

3,6)
不允许构造已知界数组。


与1)相同,但对象被默认初始化。此重载仅当T不是数组类型时才参与重载解析。该函数等同于

unique_ptr<T>(new T)

与2)相同,但数组被默认初始化。此重载仅当T是未知界数组时才参与重载解析。该函数等同于

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

参数

args - 用于构造T实例的参数列表。size - 要构造的数组的长度

返回值

类型为T的实例的std::unique_ptr

异常

可能抛出std::bad_alloc或由T的构造函数抛出的任何异常。如果抛出异常,此函数无效。

Possible Implementation

make_unique (1,2,3)

// 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;

make_unique_for_overwrite (4,5,6)

// 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;

备注

std::make_shared(它有std::allocate_shared)不同,std::make_unique没有与之对应的分配器感知版本。在P0211中提出的allocate_unique需要为它返回的std::unique_ptr<T,D>发明删除器类型D,该删除器将包含一个分配器对象并在其operator()中调用destroy和deallocate。

特性测试宏Value标准注释
__cpp_lib_make_unique201304L(C++14)std::make_unique
__cpp_lib_smart_ptr_for_overwrite202002L(C++20)for overloads (4-6)
__cpp_lib_constexpr_memory202202L(C++23)constexpr for overloads (1,2,4,5

示例

#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 }

std::make_unique, std::make_unique_for_overwrite

声明

// 3)
template< class T, class... Args >
/* unspecified */ make_unique( Args&&... args ) = delete;
// 6)
template< class T, class... Args >
/* unspecified */ make_unique_for_overwrite( Args&&... args ) = delete;

构造一个类型为T的对象,并将其包装在std::unique_ptr中。


构造一个非数组类型T。参数args将被传递给T的构造函数。此重载仅当T不是数组类型时才参与重载解析。该函数等同于

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

构造一个指定动态大小的数组。数组元素被值初始化。此重载仅当T是未知界数组时才参与重载解析。该函数等同于

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

3,6)
不允许构造已知界数组。


与1)相同,但对象被默认初始化。此重载仅当T不是数组类型时才参与重载解析。该函数等同于

unique_ptr<T>(new T)

与2)相同,但数组被默认初始化。此重载仅当T是未知界数组时才参与重载解析。该函数等同于

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

参数

args - 用于构造T实例的参数列表。size - 要构造的数组的长度

返回值

类型为T的实例的std::unique_ptr

异常

可能抛出std::bad_alloc或由T的构造函数抛出的任何异常。如果抛出异常,此函数无效。

Possible Implementation

make_unique (1,2,3)

// 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;

make_unique_for_overwrite (4,5,6)

// 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;

备注

std::make_shared(它有std::allocate_shared)不同,std::make_unique没有与之对应的分配器感知版本。在P0211中提出的allocate_unique需要为它返回的std::unique_ptr<T,D>发明删除器类型D,该删除器将包含一个分配器对象并在其operator()中调用destroy和deallocate。

特性测试宏Value标准注释
__cpp_lib_make_unique201304L(C++14)std::make_unique
__cpp_lib_smart_ptr_for_overwrite202002L(C++20)for overloads (4-6)
__cpp_lib_constexpr_memory202202L(C++23)constexpr for overloads (1,2,4,5

示例

#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 }