c++11 & 14: unique_ptr shared_ptr std::make_unique(c++14)
来源:互联网 发布:sql 注入 1 2 编辑:程序博客网 时间:2024/05/01 02:08
参考网站:
http://www.cplusplus.com/reference/utility/pair/
Both of these classes are smart pointers, which means that they automatically (in most cases) will deallocate the object that they point at when that object can no longer be referenced. The difference between the two is how many different pointers of each type can refer to a resource.
When using unique_ptr
, there can be at most one unique_ptr
pointing at any one resource. When that unique_ptr
is destroyed, the resource is automatically reclaimed. Because there can only be one unique_ptr
to any resource, any attempt to make a copy of a unique_ptr
will cause a compile-time error. For example, this code is illegal:
unique_ptr<T> myPtr(new T); // Okayunique_ptr<T> myOtherPtr = myPtr; // Error: Can't copy unique_ptr
However, unique_ptr
can be moved using the new move semantics:
unique_ptr<T> myPtr(new T); // Okayunique_ptr<T> myOtherPtr = std::move(myPtr); // Okay, resource now stored in myOtherPtr
Similarly, you can do something like this:
unique_ptr<T> MyFunction() { unique_ptr<T> myPtr(/* ... */); /* ... */ return myPtr;}
This idiom means "I'm returning a managed resource to you. If you don't explicitly capture the return value, then the resource will be cleaned up. If you do, then you now have exclusive ownership of that resource." In this way, you can think of unique_ptr
as a safer, better replacement for auto_ptr
.
shared_ptr
, on the other hand, allows for multiple pointers to point at a given resource. When the very last shared_ptr
to a resource is destroyed, the resource will be deallocated. For example, this code is perfectly legal:
shared_ptr<T> myPtr(new T); // Okayshared_ptr<T> myOtherPtr = myPtr; // Sure! Now have two pointers to the resource.
Internally, shared_ptr
uses reference counting to track how many pointers refer to a resource, so you need to be careful not to introduce any reference cycles.
In short:
- Use
unique_ptr
when you want a single pointer to an object that will be reclaimed when that single pointer is destroyed. - Use
shared_ptr
when you want multiple pointers to the same resource.
Hope this helps!
std::make_unique
<memory>
unique_ptr<T> make_unique( Args&&... args );(1)(since C++14)
(only for non-array types)template< class T >
unique_ptr<T> make_unique( std::size_t size );(2)(since C++14)
(only for array types with unknown bound)template< class T, class... Args >
/* unspecified */ make_unique( Args&&... args ) = delete;(3)(since C++14)
(only for array types with known bound)
Constructs an object of type T
and wraps it in a std::unique_ptr.
T
. The arguments args
are passed to the constructor of T
. The function does not participate in the overload resolution if T
is an array type. The function is equivalent to:unique_ptr<T>(new T(std::forward<Args>(args)...))
T
. The function does not participate in the overload resolution unless T
is an array of unknown bound. The function is equivalent to:unique_ptr<T>(new typename std::remove_extent<T>::type[size]())
Parameters
args-list of arguments with which an instance ofT
will be constructed.size-the size of the array to constructReturn value
std::unique_ptr of an instance of type T
.
Exceptions
May throw std::bad_alloc or any exception thrown by the constructor of T
. If an exception is thrown, this function has no effect.
Example
#include <iostream>#include <memory> struct Vec3{ int x, y, z; Vec3() : x(0), y(0), z(0) { } Vec3(int x, int y, int z) :x(x), y(y), z(z) { } friend std::ostream& operator<<(std::ostream& os, 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): " << '\n'; for (int i = 0; i < 5; i++) { std::cout << " " << v3[i] << '\n'; }}
Output:
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}
See also
unique_ptr
(public member function)
- c++11 & 14: unique_ptr shared_ptr std::make_unique(c++14)
- 实战c++中的智能指针unique_ptr系列-- std::unique_ptr的构造(尽量使用C++14中的std::make_unique,而不是new)
- C++11 智能指针std::shared_ptr/std::unique_ptr/std::weak_ptr
- 【C++】智能指针auto_ptr/unique_ptr/shared_ptr/weak_ptr!!!
- 关于std:auto_ptr std:shared_ptr std:unique_ptr
- std::unique_ptr和std::shared_ptr的用法
- C++14 智能指针unique_ptr、shared_ptr、weak_ptr
- C++11 auto_ptr shared_ptr unique_ptr
- c智能指针:unique_ptr
- C++11 智能指针 unique_ptr shared_ptr weak_ptr
- std::unique_ptr
- c++/c 标准库 unique_ptr
- <Effective Modern C++>Item 18: Use std::unique_ptr for exclusive-ownership resource management.
- C++11:unique_ptr 自己定义类似make_shared的make_unique模板函数
- C++11引入智能指针std::unique_ptr
- C++11智能指针之std::unique_ptr
- shared_ptr / unique_ptr不完全类型
- shared_ptr/unique_ptr一点体会
- Android SDK镜像的介绍使用
- PHP定时执行任务的实现
- 什么时候用ArrayList,什么时候用LinkedList
- 动态计算UITableViewCell高度详解 -- 学习
- 常用的APT命令参数
- c++11 & 14: unique_ptr shared_ptr std::make_unique(c++14)
- js--下拉列表选中(js和jquery两种方法 )
- 最长回文字符串
- jQuery Backstretch动态设置背景图片插件
- Android常用组件
- python正则表达式
- NTFS与FAT32区别大揭秘
- linux下 nexus搭建maven私有库 自动打包
- 【Hibernate步步为营】--(一对多映射)之单向关联