文章目录
- 0x1 基本使用
- 0x11 get() [ 参照auto_ptr ]
- 0x12 release() [ 参照auto_ptr ]
- 0x13 reset() [ 参照auto_ptr ]
- 0x14 get_deleter() [ 新增 ]
- 0x15 operator bool [ 新增 ]
- 0x16 swap() [ 新增 ]
- 0x2 make_unique函数 [unique_ptr在C++11引入,make_unique在C++14引入]
- 0x3 解决auto_ptr的迷惑问题
- 0x4 解决auto_ptr的被放弃原因
- 0x5 unique_ptr 仅仅可以解决独占资源所有权,不能解决共享资源的所有权,所以引入shared_ptr
0x1 基本使用
0x11 get() [ 参照auto_ptr ]
0x12 release() [ 参照auto_ptr ]
0x13 reset() [ 参照auto_ptr ]
0x14 get_deleter() [ 新增 ]
0x15 operator bool [ 新增 ]
可以通过nullptr判断,auto_ptr部支持
unique_ptr<string> p1(new string("HelloWorld"));
p1.release();
if (p1 == nullptr) {
cout << "p1 is nullptr" << endl;
}
0x16 swap() [ 新增 ]
unique_ptr<string> p1(new string("HelloWorld"));
unique_ptr<string> p2(new string("VsCode"));
cout << *p1 << endl;
cout << *p2 << endl;
swap(p1, p2);
cout << *p1 << endl;
cout << *p2 << endl;
0x2 make_unique函数 [unique_ptr在C++11引入,make_unique在C++14引入]
make_unique是C++14引入的一个函数模板