repost: auto_ptr与unique_ptr的关系:

from: https://blog.csdn.net/weixin_40081916/article/details/79377564
简言之: auto_ptr 用复制语法表示移动语义,和 C++约定和人的直觉不匹配.

auto_ptr与unique_ptr
auto_ptr是用于C++11之前的智能指针。由于 auto_ptr 基于排他所有权模式:两个指针不能指向同一个资源,复制或赋值都会改变资源的所有权。auto_ptr 主要有两大问题:
o 复制和赋值会改变资源的所有权,不符合人的直觉。
o 在 STL 容器中无法使用auto_ptr ,因为容器内的元素必需支持可复制(copy constructable)和可赋值(assignable)。

unique_ptr特性
o 拥有它所指向的对象
o 无法进行复制构造,也无法进行复制赋值操作
o 保存指向某个对象的指针,当它本身离开作用域时会自动释放它指向的对象。

unique_ptr可以:
o 为动态申请的内存提供异常安全
o 将动态申请内存的所有权传递给某个函数
o 从某个函数返回动态申请内存的所有权
o 在容器中保存指针
unique_ptr十分依赖于右值引用和移动语义。

在C++11中已经放弃auto_ptr转而推荐使用unique_ptr和shared_ptr。unique跟auto_ptr类似同样只能有一个智能指针对象指向某块内存.但它还有些其他特性。unique_ptr对auto_ptr的改进如下:

1, auto_ptr支持拷贝构造与赋值操作,但unique_ptr不直接支持
auto_ptr通过拷贝构造或者operator=赋值后,对象所有权转移到新的auto_ptr中去了,原来的auto_ptr对象就不再有效,这点不符合人的直觉。unique_ptr则直接禁止了拷贝构造与赋值操作。
auto_ptr ap(new int(10));
auto_ptr one (ap) ; // ok
auto_ptr two = one; //ok

unique_ptr ap(new int(10));
unique_ptr one (ap) ; // 会出错
unique_ptr two = one; //会出错

2,unique_ptr可以用在函数返回值中
unique_ptr像上面这样一般意义上的复制构造和赋值或出错,但在函数中作为返回值却可以用.
unique_ptr GetVal( ){
unique_ptr up(new int(10 );
return up;
}
unique_ptr uPtr = GetVal(); //ok

实际上上面的的操作有点类似于如下操作
unique_ptr up(new int(10);
unique_ptr uPtr2 = std:move( up) ; //这里是显式的所有权转移. 把up所指的内存转给uPtr2了,而up不再拥有该内存

3,unique_ptr可做为容器元素
我们知道auto_ptr不可做为容器元素,会导致编译错误。虽然unique_ptr同样不能直接做为容器元素,但可以通过move语意实现。
unique_ptr sp(new int(88) );
vector<unique_ptr > vec;
vec.push_back(std::move(sp));
// vec.push_back( sp ); error:
// cout << *sp << endl;
std::move让调用者明确知道拷贝构造、赋值后会导致之前的unique_ptr失效。

#include
#include
#include
using namespace std;

unique_ptr GetVal( ){
unique_ptr up(new int(10));
return up;
}

int main()
{
// auto_ptr支持拷贝构造与赋值,拷贝构造或赋值后对象所有权转移
auto_ptr aPtr(new int(10));
auto_ptr a1 (aPtr); // call copy constuctor, OK
assert(aPtr.get() == nullptr && a1.get() != nullptr);
auto_ptr a2(NULL);
a2 = a1; // call operator=, OK
assert(a1.get() == nullptr && a2.get() != nullptr);

// unique_ptr不支持直接拷贝构造与赋值
unique_ptr<int> uPtr(new int(10));
// unique_ptr<int> u1 (uPtr);       // call copy constructor, error 
// Error: Call to implicitly-deleted copy constructor of 'unique_ptr<int>'

// unique_ptr<int> u2 = nullptr;
// u2 = uPtr;               // call operator=, error
// Error: Overload resolution selected implicitly-deleted copy assignment operator

// unique_ptr支持move语意的拷贝构造
unique_ptr<int> u1(std::move(uPtr));
assert(uPtr == nullptr && u1 != nullptr);

// unique_ptr支持move语意的赋值
unique_ptr<int> u2 = std::move(u1) ; // 把u1所指的内存转给u2了,而u1不再拥有该内存
assert(u1 == nullptr && u2 != nullptr);

// unique_ptr通过move语意可放入STL容器
unique_ptr<int> sp(new int(100));
vector<unique_ptr<int>> vec;
vec.push_back(std::move(sp));

// vec.push_back(sp);
// Error: Call to implicitly-deleted copy constructor of 'std::__1::unique_ptr<int, std::__1::default_delete<int> >'

assert(sp == nullptr);
// cout << *sp << endl;  // crash at this point, sp is nullptr now

unique_ptr<int> g_uPtr = GetVal();   //ok

}

猜你喜欢

转载自blog.csdn.net/wc996789331/article/details/113433365
今日推荐