首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::unique_ptr::unique_ptr

members of the primary template, unique_ptr<T>

constexpr unique_ptr(); constexpr unique_ptr( nullptr_t );

(1)

explicit unique_ptr( pointer p );

(2)

unique_ptr( pointer p, /* see below */ d1 );

(3)

unique_ptr( pointer p, /* see below */ d2 );

(4)

unique_ptr( unique_ptr&& u );

(5)

template< class U, class E > unique_ptr( unique_ptr<U, E>&& u );

(6)

template< class U > unique_ptr( auto_ptr<U>&& u );

(7)

(until C++17)

members of the specialization for arrays, unique_ptr<T[]>

constexpr unique_ptr(); constexpr unique_ptr( nullptr_t );

(1)

explicit unique_ptr( pointer p );

(2)

(until C++17)

template<class U> explicit unique_ptr( U p );

(2)

(since C++17)

unique_ptr( pointer p, /* see below */ d1 );

(3)

(until C++17)

template<class U> unique_ptr( U p, /* see below */ d1 );

(3)

(since C++17)

unique_ptr( pointer p, /* see below */ d2 );

(4)

(until C++17)

template<class U> unique_ptr( U p, /* see below */ d2 );

(4)

(since C++17)

unique_ptr( unique_ptr&& u );

(5)

template< class U, class E > unique_ptr( unique_ptr<U, E>&& u );

(6)

(since C++17)

1%29构造一个std::unique_ptrDeleterDefaultConstructible而这种结构并不会抛出一个例外。

This constructor is ill-formed if Deleter is of pointer or reference type.

(until C++17)

These overloads only participate in overload resolution if std::is_default_constructible<Deleter>::value is true and Deleter is not a pointer type.

(since C++17)

2%29构造一个std::unique_ptr它拥有p,用p初始化存储的指针,值初始化存储的删除项.。要求DeleterDefaultConstructible而这种结构并不会抛出一个例外。

This constructor is ill-formed if Deleter is of pointer or reference type.

(until C++17)

This overload only participates in overload resolution if std::is_default_constructible<Deleter>::value is true and Deleter is not a pointer type.

(since C++17)

3-4%29构造astd::unique_ptr拥有p初始化存储的指针。p和初始化删除器D低于%28取决于D是引用类型%29

A%29如果D是非参考类型A,然后签名是:unique_ptr(pointer p, const A& d);%28要求Deleter不是扔-CopyConstructible%29unique_ptr(pointer p, A&& d);%28要求Deleter不是扔-MoveConstructible%29

B%29D是值引用类型。A&,然后签名是:unique_ptr(pointer p, A& d);unique_ptr(pointer p, A&& d);

C%29如果D是值引用类型。const A&,然后签名是:unique_ptr(pointer p, const A& d);unique_ptr(pointer p, const A&& d);

在所有情况下,删除程序都是从std::forward<decltype(d)>(d)...

If D is a reference type and the second overload is chosen, the program is ill-formed.

(until C++17)

If D is a reference type, the second overload is defined as deleted. These overloads only participate in overload resolution if std::is_constructible<D, decltype(d)>::value is true.

(since C++17)

2-4) in the specialization for arrays behave the same as the constructors that take a pointer parameter in the primary template except that they additionally do not participate in overload resolution unless one of the following is true: U is the same type as pointer, or U is std::nullptr_t, or pointer is the same type as element_type* and U is some pointer type V* such that V(*)[] is implicitly convertible to element_type(*)[].

(since C++17)

5%29构造一个unique_ptr通过转移所有权u*this.如果Deleter不是引用类型,要求它不是抛出-MoveConstructible%28Deleter是一个参考,get_deleter()u.get_deleter()移动施工后引用相同值%29

6%29构造一个unique_ptr通过转移所有权u*this,在哪里u是用指定的删除项%28构造的。E29%。这取决于E是引用类型,如下所示:

A%29如果E是引用类型,此删除项是从u%27s删除%28要求此结构不抛出%29

B%29E是一个非引用类型,此删除项是从u%27s删除%28要求此结构不抛出%29

此构造函数仅参与重载解决方案,如果以下所有内容都为真:

A%29unique_ptr<U, E>::pointer隐式可转换为pointer

B%29 U不是数组类型

C%29Deleter是引用类型,并且E是与D,或Deleter is not a reference typeE隐式可转换为D

6) in the specialization for arrays behaves the same as in the primary template, except that it will only participate in overload resolution if all of the following is true U is an array type pointer is the same type as element_type* unique_ptr<U,E>::pointer is the same type as unique_ptr<U,E>::element_type* unique_ptr<U,E>::element_type(*)[] is convertible to element_type(*)[] either Deleter is a reference type and E is the same type as Deleter, or Deleter is not a reference type and E is implicitly convertible to Deleter.

(since C++17)

7%29构造一个unique_ptr初始化存储指针的位置。u.release()存储的删除器是值初始化的。此构造函数仅参与重载解决方案。U*隐式可转换为T*Deleter是与std::default_delete<T>...

参数

p

-

a pointer to an object to manage

d1,d2

-

a deleter to use to destroy the object

u

-

another smart pointer to acquire the ownership from

例外

noexcept规格:

noexcept

注记

与其使用重载%282%29和New,不如使用std::make_unique<T>...

std::unique_ptr<Derived>隐式可转换为std::unique_ptr<Base>通过重载%286%29%28,因为托管指针和std::default_delete隐式可兑换%29。

因为默认构造函数是constexpr,静态唯一[医]PTRS被初始化为静态非局部初始化,在任何动态的非本地初始化开始之前。这使得使用唯一的[医]任何静态对象的构造函数中的PTR。

二次

代码语言:javascript
复制
#include <iostream>
#include <memory>
 
struct Foo { // object to manage
    Foo() { std::cout << "Foo ctor\n"; }
    Foo(const Foo&) { std::cout << "Foo copy ctor\n"; }
    Foo(Foo&&) { std::cout << "Foo move ctor\n"; }
    ~Foo() { std::cout << "~Foo dtor\n"; }
};
 
struct D { // deleter
    D() {};
    D(const D&) { std::cout << "D copy ctor\n"; }
    D(D&) { std::cout << "D non-const copy ctor\n";}
    D(D&&) { std::cout << "D move ctor \n"; }
    void operator()(Foo* p) const {
        std::cout << "D is deleting a Foo\n";
        delete p;
    };
};
 
int main()
{
    std::cout << "Example constructor(1)...\n";
    std::unique_ptr<Foo> up1;  // up1 is empty
    std::unique_ptr<Foo> up1b(nullptr);  // up1b is empty
 
    std::cout << "Example constructor(2)...\n";
    {
        std::unique_ptr<Foo> up2(new Foo); //up2 now owns a Foo
    } // Foo deleted
 
    std::cout << "Example constructor(3)...\n";
    D d;
    {  // deleter type is not a reference
       std::unique_ptr<Foo, D> up3(new Foo, d); // deleter copied
    }
    {  // deleter type is a reference 
       std::unique_ptr<Foo, D&> up3b(new Foo, d); // up3b holds a reference to d
    }
 
    std::cout << "Example constructor(4)...\n";
    {  // deleter is not a reference 
       std::unique_ptr<Foo, D> up4(new Foo, D()); // deleter moved
    }
 
    std::cout << "Example constructor(5)...\n";
    {
       std::unique_ptr<Foo> up5a(new Foo);
       std::unique_ptr<Foo> up5b(std::move(up5a)); // ownership transfer
    }
 
    std::cout << "Example constructor(6)...\n";
    {
        std::unique_ptr<Foo, D> up6a(new Foo, d); // D is copied
        std::unique_ptr<Foo, D> up6b(std::move(up6a)); // D is moved
 
        std::unique_ptr<Foo, D&> up6c(new Foo, d); // D is a reference
        std::unique_ptr<Foo, D> up6d(std::move(up6c)); // D is copied
    }
 
    std::cout << "Example constructor(7)...\n";
    {
        std::auto_ptr<Foo> up7a(new Foo);
        std::unique_ptr<Foo> up7b(std::move(up7a)); // ownership transfer
    }
}

二次

产出:

二次

代码语言:javascript
复制
Example constructor(1)...
Example constructor(2)...
Foo ctor
~Foo dtor
Example constructor(3)...
Foo ctor
D copy ctor
D is deleting a Foo
~Foo dtor
Foo ctor
D is deleting a Foo
~Foo dtor
Example constructor(4)...
Foo ctor
D move ctor 
D is deleting a Foo
~Foo dtor
Example constructor(5)...
Foo ctor
~Foo dtor
Example constructor(6)...
Foo ctor
D copy ctor
D move ctor 
Foo ctor
D non-const copy ctor
D is deleting a Foo
~Foo dtor
D is deleting a Foo
~Foo dtor
Example constructor(7)...
Foo ctor
~Foo dtor

二次

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券