Namespaces
Variants

std:: make_unique, std:: make_unique_for_overwrite

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
定义于头文件 <memory>
(1)
template < class T, class ... Args >
unique_ptr < T > make_unique ( Args && ... args ) ;
(C++14 起)
(C++23 前)
(仅针对非数组类型)
template < class T, class ... Args >
constexpr unique_ptr < T > make_unique ( Args && ... args ) ;
(C++23 起)
(仅针对非数组类型)
(2)
template < class T >
unique_ptr < T > make_unique ( std:: size_t size ) ;
(C++14 起)
(C++23 前)
(仅针对未知边界的数组类型)
template < class T >
constexpr unique_ptr < T > make_unique ( std:: size_t size ) ;
(C++23 起)
(仅针对未知边界的数组类型)
template < class T, class ... Args >
/* 未指定 */ make_unique ( Args && ... args ) = delete ;
(3) (C++14 起)
(仅针对已知边界的数组类型)
(4)
template < class T >
unique_ptr < T > make_unique_for_overwrite ( ) ;
(C++20 起)
(C++23 前)
(仅针对非数组类型)
template < class T >
constexpr unique_ptr < T > make_unique_for_overwrite ( ) ;
(C++23 起)
(仅针对非数组类型)
(5)
template < class T >
unique_ptr < T > make_unique_for_overwrite ( std:: size_t size ) ;
(C++20 起)
(C++23 前)
(仅针对未知边界的数组类型)
template < class T >
constexpr unique_ptr < T > make_unique_for_overwrite ( std:: size_t size ) ;
(C++23 起)
(仅针对未知边界的数组类型)
template < class T, class ... Args >
/* 未指定 */ make_unique_for_overwrite ( Args && ... args ) = delete ;
(6) (C++20 起)
(仅针对已知边界的数组类型)

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

1) 构造一个非数组类型 T 。参数 args 被传递给 T 的构造函数。此重载仅当 T 不是数组类型时参与重载决议。该函数等价于:
unique_ptr<T>(new T(std::forward<Args>(args)...))
2) 构造具有给定动态大小的数组。数组元素将进行 值初始化 。仅当 T 是未知边界数组时,此重载才参与重载决议。该函数等效于:
unique_ptr<T>(new std::remove_extent_t<T>[size]())
3,6) 禁止构造已知边界的数组。
4) (1) 相同,但对象会进行 默认初始化 。仅当 T 不是数组类型时,此重载参与重载决议。该函数等价于:
unique_ptr<T>(new T)
5) (2) 相同,但数组是默认初始化的。此重载仅当 T 是未知边界数组时才参与重载决议。该函数等效于:
unique_ptr<T>(new std::remove_extent_t<T>[size])

目录

参数

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

返回值

std::unique_ptr 的一个类型为 T 的实例。

异常

可能抛出 std::bad_alloc T 构造函数抛出的任何异常。若抛出异常,则此函数不产生任何效果。

可能的实现

make_unique (1-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-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 方法。

功能测试 标准 功能
__cpp_lib_make_unique 201304L (C++14) std::make_unique ;重载版本 ( 1 )
__cpp_lib_smart_ptr_for_overwrite 202002L (C++20) 支持默认初始化的智能指针创建( std::allocate_shared_for_overwrite std::make_shared_for_overwrite std::make_unique_for_overwrite );重载版本 ( 4-6 )
__cpp_lib_constexpr_memory 202202L (C++23) 重载版本的 constexpr 支持 ( 1,2,4,5 )

示例

#include <cstddef>
#include <iomanip>
#include <iostream>
#include <memory>
#include <utility>
struct Vec3
{
    int x, y, z;
    // 自 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 << " }";
    }
};
// 将斐波那契数输出到输出迭代器。
template<typename OutputIt>
OutputIt fibonacci(OutputIt first, OutputIt last)
{
    for (int a = 0, b = 1; first != last; ++first)
    {
        *first = b;
        b += std::exchange(a, b);
    }
    return first;
}
int main()
{
    // 使用默认构造函数。
    std::unique_ptr<Vec3> v1 = std::make_unique<Vec3>();
    // 使用匹配这些参数的构造函数。
    std::unique_ptr<Vec3> v2 = std::make_unique<Vec3>(0, 1, 2);
    // 创建一个指向包含 5 个元素的数组的 unique_ptr。
    std::unique_ptr<Vec3[]> v3 = std::make_unique<Vec3[]>(5);
    // 创建一个指向包含 10 个整数的未初始化数组的 unique_ptr,
    // 然后用斐波那契数填充它。
    std::unique_ptr<int[]> i1 = std::make_unique_for_overwrite<int[]>(10);
    fibonacci(i1.get(), i1.get() + 10);
    std::cout << "make_unique<Vec3>():      " << *v1 << '\n'
              << "make_unique<Vec3>(0,1,2): " << *v2 << '\n'
              << "make_unique<Vec3[]>(5):   ";
    for (std::size_t i = 0; i < 5; ++i)
        std::cout << std::setw(i ? 30 : 0) << v3[i] << '\n';
    std::cout << '\n';
    std::cout << "make_unique_for_overwrite<int[]>(10), fibonacci(...): [" << i1[0];
    for (std::size_t i = 1; i < 10; ++i)
        std::cout << ", " << i1[i];
    std::cout << "]\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 }
make_unique_for_overwrite<int[]>(10), fibonacci(...): [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]

参见

构造新的 unique_ptr
(公开成员函数)
创建管理新对象的共享指针
(函数模板)