std::unique_ptr::unique_ptr
来自cppreference.com
< cpp | memory | unique ptr
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
constexpr unique_ptr(); |
(1) | (C++11 起) |
explicit unique_ptr( pointer p ); |
(2) | (C++11 起) |
unique_ptr( pointer p, d1 ); |
(3) | (C++11 起) |
unique_ptr( pointer p, d2 ); |
(4) | (C++11 起) |
unique_ptr( unique_ptr&& u ); |
(5) | (C++11 起) |
constexpr unique_ptr( nullptr_t ); |
(6) | (C++11 起) |
template< class U, class E > unique_ptr( unique_ptr<U, E>&& u ); |
(7) | (C++11 起) |
template< class U > unique_ptr( auto_ptr<U>&& u ); |
(8) | (C++11 起) |
1)
构造一个空的
std::unique_ptr
.2)
构造一个
std::unique_ptr
,拥有p,初始化所存储的指针p和值初始化存储的删除.原文:
Constructs a
std::unique_ptr
which owns p, initializing the stored pointer with p and value-initializing the stored deleter.3-4)
构造一个
std::unique_ptr
对象,拥有p
,初始化存储的指针p
初始化的删除D
如下(取决于是否D
是一个引用类型)原文:
Constructs a
std::unique_ptr
object which owns p
, initializing the stored pointer with p
and initializing a deleter D
as below (depends upon whether D
is a reference type)a)
如果
D
是非引用类型A,然后签名是:原文:
If
D
is non-reference type A, then the signatures are:-
unique_ptr(pointer p, const A& d);
-
unique_ptr(pointer p, A&& d);
b)
如果
D
是一个左值的引用类型A&,然后签名原文:
If
D
is an lvalue-reference type A&, then the signatures are:-
unique_ptr(pointer p, A& d);
-
unique_ptr(pointer p, A&& d);
c)
如果
D
是一个左值的引用类型const A&,然后签名原文:
If
D
is an lvalue-reference type const A&, then the signatures are:-
unique_ptr(pointer p, const A& d);
-
unique_ptr(pointer p, const A&& d);
5)
构造一个
unique_ptr
的所有权转让从u
*this原文:
Constructs a
unique_ptr
by transferring ownership from u
to *this.6)
构造一个空的
std::unique_ptr
.7)
构造一个
unique_ptr
u
*this,u
构造一个指定的删除(E
)的所有权转让。这取决于是否E
是一个引用类型,如下:原文:
Constructs a
unique_ptr
by transferring ownership from u
to *this, where u
is constructed with a specified deleter (E
). It depends upon whether E
is a reference type, as following:a)
E
是一个引用类型,这Deleter的是u
的Deleter的拷贝构造.原文:
if
E
is a reference type, this deleter is copy constructed from u
's deleter.b)
如果
E
是一个非引用类型,当前的删除是从u
的删除器构造的移动.原文:
if
E
is a non-reference type, this deleter is move constructed from u
's deleter.8)
构造一个
unique_ptr
所存储的指针被初始化u.release()
和存储的删除是值初始化.原文:
Constructs a
unique_ptr
where the stored pointer is initialized with u.release()
and the stored deleter is value-initialized.[编辑] 参数
p | - | 一个指针,指向一个对象管理
|
d1,d2 | - | 使用一个的删除销毁对象
原文: a deleter to use to destroy the object |
u | - | 另一个智能指针,从获得的所有权
原文: another smart pointer to acquire the ownership from |
[编辑] 例外
[编辑] 示例
#include <iostream> #include <memory> struct Foo { Foo() { std::cout << "Foo...\n"; } ~Foo() { std::cout << "~Foo...\n\n"; } }; struct D{ D(){}; D(const D& other){ std::cout << "call D const copy constructor... \n"; } D(D& other){ std::cout << "call D copy constructor... \n"; } D(D&& other){ std::cout << "call D move constructor... \n"; } void operator () (Foo* p) const { std::cout << "Call delete for Foo object...\n"; delete p; }; }; int main() { //constructor (1) std::cout << "Example constructor(1)...\n\n"; std::unique_ptr<Foo> up; //constructor (2) std::cout << "Example constructor(2)...\n"; Foo* f = new Foo(); std::unique_ptr<Foo> up2(f); //up2 now owns f up2.reset(); //constructor (3&4) std::cout << "Example constructor(3&4)...\n"; //D is not an lvalue-reference - d is a non-const rvalue std::unique_ptr<Foo, D> up3(new Foo(), D()); //D must be MoveConstructible up3.reset(); //D is not an lvalue-refernce - d is a left value D d2; std::unique_ptr<Foo, D> up4(new Foo(), d2); //D must be Copyconstructible up4.reset(); //D is a left value reference type D d3; std::unique_ptr<Foo, D&> up5(new Foo(), d3); //up3 holds a reference to d3 up5.reset(); //D is a const left value reference type const D d4; std::unique_ptr<Foo, const D&> up6(new Foo(), d4); up6.reset(); //constructor (5) std::cout << "Example constructor(5)...\n"; std::unique_ptr<Foo> up7(new Foo()); std::unique_ptr<Foo> up8(move(up7)); //ownership is transfered to up8 up8.reset(); //constructor 6 std::cout << "Example constructor(6)...\n\n"; std::unique_ptr<Foo> up9(nullptr); //constructor 7 - D is move constructed D d; std::cout << "Example constructor(7)...\n"; std::unique_ptr<Foo, D> up10(new Foo(), d); //D is not a reference std::unique_ptr<Foo, D> up11(move(up10)); //D is move constructed up11.reset(); //constructor 7 - D is copy constructed std::unique_ptr<Foo, D&> up12(new Foo(), d); //D is a reference std::unique_ptr<Foo, D> up13(move(up12)); //D is copy constructed up13.reset(); //constructor 8 std::cout << "Example constructor(8)...\n"; std::auto_ptr<Foo> up14(new Foo()); std::unique_ptr<Foo> up15(move(up14)); up15.reset(); }