@zhengyuhong
2015-06-09T07:14:06.000000Z
字数 4880
阅读 1106
C++11 STL Boost
unique_ptr不仅能够管理new创建单个对象,也能够管理new []创建到数组对象。它是一个独享所有权的智能指针,它提供了一种严格语义上的所有权,包括:
1. 拥有它所指向的对象。
2. 无法进行复制构造,也无法进行复制赋值操作。也就是说,我们无法得到指向同一个对象的两个unique_ptr。但是可以进行移动构造和移动赋值操作。
3. 3、保存指向某个对象的指针,当它本身被删除释放的时候(比如,离开了某个作用域),会使用给定的删除器释放它指向的对象。
template <class T, class D = default_delete<T>>class unique_ptr;template <class T, class D>class unique_ptr<T[],D>;
constexpr unique_ptr() noexcept;constexpr unique_ptr (nullptr_t) noexcept : unique_ptr() {}explicit unique_ptr (pointer p) noexcept;
unique_ptr (pointer p,typename conditional<is_reference<D>::value,D,const D&> del) noexcept;unique_ptr (pointer p,typename remove_reference<D>::type&& del) noexcept;
unique_ptr (unique_ptr&& x) noexcept;template <class U, class E>unique_ptr (unique_ptr<U,E>&& x) noexcept;template <class U>unique_ptr (auto_ptr<U>&& x) noexcept;
unique_ptr (const unique_ptr&) = delete;
pointer get() const noexcept;//Returns the stored pointer.Not realeasepointer release() noexcept;//Releases ownership of its stored pointer, by returning its value and replacing it with a null pointer.void reset (pointer p = pointer()) noexcept;//Destroys the object currently managed by the unique_ptr (if any) and takes ownership of p.explicit operator bool() const noexcept;//Check if not emptyReturns whether an object is currently managed by the unique_ptr (i.e., whether the unique_ptr is not empty).
deleter_type& get_deleter() noexcept;const deleter_type& get_deleter() const noexcept;
example:
#include <iostream>#include <memory>class Del_c {public:void operator()(int* p){std::cout<<"del"<<std::endl;delete p;p = nulptr;}};void del_func(int* p) {std::cout<<"del"<<std::endl;delete p;p = nulptr;}auto del_l = [](int* p){std::cout<<"del"<<std::endl;delete p;p = nulptr;}int main () {std::unique_ptr<int> p; // uses default deleterauto del1 = Del_c();auto del2 = del_func;auto del3 = del_l;// alpha and beta use independent copies of the deleter:std::unique_ptr<int> alpha (new int);std::unique_ptr<int,delctype(del1)> beta (new int,del1);//del2,del3return 0;}
deleter的要求是参数为T* p的可调用函数、实例、lamdba表达式
template <class T> class shared_ptr;
constexpr shared_ptr() noexcept;constexpr shared_ptr(nullptr_t) : shared_ptr() {}template <class U> explicit shared_ptr (U* p);
template <class U, class D> shared_ptr (U* p, D del);template <class D> shared_ptr (nullptr_t p, D del);
// shared_ptr constructor example#include <iostream>#include <memory>struct C {int* data;};int main () {std::shared_ptr<int> p1;std::shared_ptr<int> p2 (nullptr);std::shared_ptr<int> p3 (new int);std::shared_ptr<int> p4 (new int, std::default_delete<int>());std::shared_ptr<int> p5 (new int, [](int* p){delete p;}, std::allocator<int>());std::shared_ptr<int> p6 (p5);std::shared_ptr<int> p7 (std::move(p6));std::shared_ptr<int> p8 (std::unique_ptr<int>(new int));std::shared_ptr<C> obj (new C);std::shared_ptr<int> p9 (obj, obj->data);std::cout << "use_count:\n";std::cout << "p1: " << p1.use_count() << '\n';std::cout << "p2: " << p2.use_count() << '\n';std::cout << "p3: " << p3.use_count() << '\n';std::cout << "p4: " << p4.use_count() << '\n';std::cout << "p5: " << p5.use_count() << '\n';std::cout << "p6: " << p6.use_count() << '\n';std::cout << "p7: " << p7.use_count() << '\n';std::cout << "p8: " << p8.use_count() << '\n';std::cout << "p9: " << p9.use_count() << '\n';return 0;}
long int use_count() const noexcept;
Returns the number of shared_ptr objects that share ownership over the same pointer as this object (including it).
template <class T> class weak_ptr;
指向共享资源的指针,但是不增加引用计数,用于监察shared_ptr状态
constexpr weak_ptr() noexcept;weak_ptr (const weak_ptr& x) noexcept;template <class U>weak_ptr (const weak_ptr<U>& x) noexcept;template <class U>weak_ptr (const shared_ptr<U>& x) noexcept;
#include <iostream>#include <memory>int main () {std::shared_ptr<int> sp (new int);std::weak_ptr<int> wp1;std::weak_ptr<int> wp2 (wp1);std::weak_ptr<int> wp3 (sp);std::cout << "use_count:\n";std::cout << "wp1: " << wp1.use_count() << '\n';std::cout << "wp2: " << wp2.use_count() << '\n';std::cout << "wp3: " << wp3.use_count() << '\n';return 0;}
void reset() noexcept;//The object becomes empty, as if default constructed.
shared_ptr<element_type> lock() const noexcept;
Lock and restore weak_ptr.Returns a shared_ptr with the information preserved by the weak_ptr object if it is not expired.
bool expired() const noexcept;
Check if expired.Returns whether the weak_ptr object is either empty or there are no more shared_ptr in the owner group it belongs to.
template <class T, class... Args>shared_ptr<T> make_shared (Args&&... args);
Allocates and constructs an object of type T passing args to its constructor, and returns an object of type shared_ptr that owns and stores a pointer to it (with a use count of 1).
argsList of elements passed to T's constructor.Args is a list of zero or more types.
example
#include <iostream>#include <memory>int main () {std::shared_ptr<int> foo = std::make_shared<int> (10);// same as:std::shared_ptr<int> foo2 (new int(10));auto bar = std::make_shared<int> (20);auto baz = std::make_shared<std::pair<int,int>> (30,40);std::cout << "*foo: " << *foo << '\n';std::cout << "*bar: " << *bar << '\n';std::cout << "*baz: " << baz->first << ' ' << baz->second << '\n';return 0;}