std::unique_ptr
Defined in header <memory> | | |
|---|---|---|
template< class T, class Deleter = std::default_delete<T> > class unique_ptr; | (1) | (since C++11) |
template < class T, class Deleter > class unique_ptr<T[], Deleter>; | (2) | (since C++11) |
std::unique_ptr是一个智能指针,它通过指针拥有和管理另一个对象,并在unique_ptr超出了范围。
当发生下列任何一种情况时,将使用关联的删除器释放该对象:
- 管理
unique_ptr对象被销毁
- 管理
unique_ptr对象通过以下方式分配另一个指针:operator=或reset()...
对象通过调用get_deleter()(ptr)默认删除器使用delete运算符,它破坏对象并释放内存。
阿unique_ptr也可以不拥有任何对象,在这种情况下,它被调用。空空...
有两个版本的std::unique_ptr*
1%29管理单个对象%28例如。分配给new%29
2%29管理一个动态分配的对象数组%28例如。分配给new[]%29
该类满足MoveConstructible和MoveAssignable,但这两种方法的要求都不同。CopyConstructible或CopyAssignable...
类型要求
*。
-Deleter必须是FunctionObject或lvalue引用FunctionObject或lvalue引用函数,可调用类型为唯一的参数。[医]PTR<T,Deleter>::指针
注记
只有非Constunique_ptr可以将托管对象的所有权传递给另一个对象。unique_ptr如果对象%27s生存期由const std::unique_ptr,它仅限于创建指针的作用域。
std::unique_ptr通常用于管理对象的生存期,包括:
- 为处理具有动态生存期的对象的类和函数提供异常安全,方法是确保在正常退出和通过异常退出时都进行删除。
- 将具有动态生存期的唯一拥有对象的所有权传递给函数。
- 从函数中获取具有动态生存期的唯一拥有对象的所有权
- 作为移动感知容器中的元素类型,如
std::vector,它保存指向动态分配对象的指针%28例如。如果需要多态行为%29
std::unique_ptr可以为不完全类型T类中的句柄使用。PIMPL成语如果使用默认删除器,T必须在调用删除器的代码点完成,这发生在析构函数、移动赋值运算符和reset成员函数std::unique_ptr.%28相反,std::shared_ptr可以从指向不完全类型的原始指针构造%27T,但可以在以下位置销毁T是不完整的%29。注意如果T是类模板专门化,使用unique_ptr作为操作数,例如。!p要求T%27s参数由于ADL...
如果T是派生类某一基地B,然后std::unique_ptr<T>是隐式可兑换到std::unique_ptr<B>的默认删除项。std::unique_ptr<B>将使用操作符删除为B,导致未定义行为除非B是虚拟注意std::shared_ptr行为不同:std::shared_ptr<B>将使用操作符删除类型T的析构函数也将正确删除所拥有的对象。B不是虚拟...
不像std::shared_ptr,,,std::unique_ptr可以通过满足以下条件的任何自定义句柄类型来管理对象NullablePointer例如,通过提供Deleter定义typedefboost::offset_ptrpointer;或者另一个花哨指针...
成员类型
Member type | Definition |
|---|---|
pointer | std::remove_reference<Deleter>::type::pointer if that type exists, otherwise T*. Must satisfy NullablePointer |
element_type | T, the type of the object managed by this unique_ptr |
deleter_type | Deleter, the function object or lvalue reference to function or to function object, to be called from the destructor |
成员函数
(constructor) | constructs a new unique_ptr (public member function) |
|---|---|
(destructor) | destructs the managed object if such is present (public member function) |
operator= | assigns the unique_ptr (public member function) |
修饰符
释放返回一个指向托管对象的指针,并释放所有权%28公共成员函数%29。
重置替换托管对象%28公共成员函数%29
交换托管对象%28公共成员函数%29
观察员
GET返回指向托管对象%28公共成员函数%29的指针
弄到[医]Deleter返回用于销毁托管对象%28公共成员函数%29的删除器。
操作符bool检查是否有关联的托管对象%28公共成员函数%29
单目标版本,唯一的[医]PTR<T>
操作者%2A运算符->指示托管对象%28公共成员函数%29的退出指针
数组版本,唯一的[医]PTR<T。[]>
操作者。[]提供对托管数组%28公共成员函数%29的索引访问。
非会员职能
make_unique (C++14) | creates a unique pointer that manages a new object (function template) |
|---|---|
operator==operator!=operator<operator<=operator>operator>= | compares to another unique_ptr or with nullptr (function template) |
std::swap(std::unique_ptr) (C++11) | specializes the std::swap algorithm (function template) |
帮助者类
std::hash<std::unique_ptr> (C++11) | hash support for std::unique_ptr (class template specialization) |
|---|
例
二次
#include <iostream>
#include <vector>
#include <memory>
#include <cstdio>
#include <fstream>
#include <cassert>
struct B {
virtual void bar() { std::cout << "B::bar\n"; }
virtual ~B() = default;
};
struct D : B
{
D() { std::cout << "D::D\n"; }
~D() { std::cout << "D::~D\n"; }
void bar() override { std::cout << "D::bar\n"; }
};
// a function consuming a unique_ptr can take it by value or by rvalue reference
std::unique_ptr<D> pass_through(std::unique_ptr<D> p)
{
p->bar();
return p;
}
int main()
{
std::cout << "unique ownership semantics demo\n";
{
auto p = std::make_unique<D>(); // p is a unique_ptr that owns a D
auto q = pass_through(std::move(p));
assert(!p); // now p owns nothing and holds a null pointer
q->bar(); // and q owns the D object
} // ~D called here
std::cout << "Runtime polymorphism demo\n";
{
std::unique_ptr<B> p = std::make_unique<D>(); // p is a unique_ptr that owns a D
// as a pointer to base
p->bar(); // virtual dispatch
std::vector<std::unique_ptr<B>> v; // unique_ptr can be stored in a container
v.push_back(std::make_unique<D>());
v.push_back(std::move(p));
v.emplace_back(new D);
for(auto& p: v) p->bar(); // virtual dispatch
} // ~D called 3 times
std::cout << "Custom deleter demo\n";
std::ofstream("demo.txt") << 'x'; // prepare the file to read
{
std::unique_ptr<std::FILE, decltype(&std::fclose)> fp(std::fopen("demo.txt", "r"),
&std::fclose);
if(fp) // fopen could have failed; in which case fp holds a null pointer
std::cout << (char)std::fgetc(fp.get()) << '\n';
} // fclose() called here, but only if FILE* is not a null pointer
// (that is, if fopen succeeded)
std::cout << "Custom lambda-expression deleter demo\n";
{
std::unique_ptr<D, std::function<void(D*)>> p(new D, [](D* ptr)
{
std::cout << "destroying from a custom deleter...\n";
delete ptr;
}); // p owns D
p->bar();
} // the lambda above is called and D is destroyed
std::cout << "Array form of unique_ptr demo\n";
{
std::unique_ptr<D[]> p{new D[3]};
} // calls ~D 3 times
}二次
产出:
二次
unique ownership semantics demo
D::D
D::bar
D::bar
D::~D
Runtime polymorphism demo
D::D
D::bar
D::D
D::D
D::bar
D::bar
D::bar
D::~D
D::~D
D::~D
Custom deleter demo
x
Custom lambda-expression deleter demo
D::D
D::bar
destroying from a custom deleter...
D::~D
Array form of unique_ptr demo
D::D
D::D
D::D
D::~D
D::~D
D::~D二次
© cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

