[关闭]
@zhengyuhong 2015-06-09T07:14:06.000000Z 字数 4880 阅读 1106

memory

C++11 STL Boost


unique_ptr

  unique_ptr不仅能够管理new创建单个对象,也能够管理new []创建到数组对象。它是一个独享所有权的智能指针,它提供了一种严格语义上的所有权,包括:
  1. 拥有它所指向的对象。
  2. 无法进行复制构造,也无法进行复制赋值操作。也就是说,我们无法得到指向同一个对象的两个unique_ptr。但是可以进行移动构造和移动赋值操作。
  3. 3、保存指向某个对象的指针,当它本身被删除释放的时候(比如,离开了某个作用域),会使用给定的删除器释放它指向的对象。

  1. template <class T, class D = default_delete<T>>
  2. class unique_ptr;
  3. template <class T, class D>
  4. class unique_ptr<T[],D>;

unique_ptr::unique_ptr

  1. constexpr unique_ptr() noexcept;
  2. constexpr unique_ptr (nullptr_t) noexcept : unique_ptr() {}
  3. explicit unique_ptr (pointer p) noexcept;
  1. unique_ptr (pointer p,
  2. typename conditional<is_reference<D>::value,D,const D&> del) noexcept;
  3. unique_ptr (pointer p,
  4. typename remove_reference<D>::type&& del) noexcept;
  1. unique_ptr (unique_ptr&& x) noexcept;
  2. template <class U, class E>
  3. unique_ptr (unique_ptr<U,E>&& x) noexcept;
  4. template <class U>
  5. unique_ptr (auto_ptr<U>&& x) noexcept;
  1. unique_ptr (const unique_ptr&) = delete;

unique_ptr::public member function

  1. pointer get() const noexcept;
  2. //Returns the stored pointer.Not realease
  3. pointer release() noexcept;
  4. //Releases ownership of its stored pointer, by returning its value and replacing it with a null pointer.
  5. void reset (pointer p = pointer()) noexcept;
  6. //Destroys the object currently managed by the unique_ptr (if any) and takes ownership of p.
  7. explicit operator bool() const noexcept;
  8. //Check if not empty
  9. Returns whether an object is currently managed by the unique_ptr (i.e., whether the unique_ptr is not empty).
  1. deleter_type& get_deleter() noexcept;
  2. const deleter_type& get_deleter() const noexcept;

example:

  1. #include <iostream>
  2. #include <memory>
  3. class Del_c {
  4. public:
  5. void operator()(int* p){
  6. std::cout<<"del"<<std::endl;
  7. delete p;
  8. p = nulptr;
  9. }
  10. };
  11. void del_func(int* p) {
  12. std::cout<<"del"<<std::endl;
  13. delete p;
  14. p = nulptr;
  15. }
  16. auto del_l = [](int* p){
  17. std::cout<<"del"<<std::endl;
  18. delete p;
  19. p = nulptr;
  20. }
  21. int main () {
  22. std::unique_ptr<int> p; // uses default deleter
  23. auto del1 = Del_c();
  24. auto del2 = del_func;
  25. auto del3 = del_l;
  26. // alpha and beta use independent copies of the deleter:
  27. std::unique_ptr<int> alpha (new int);
  28. std::unique_ptr<int,delctype(del1)> beta (new int,del1);//del2,del3
  29. return 0;
  30. }

deleter的要求是参数为T* p的可调用函数、实例、lamdba表达式

share_ptr

  1. template <class T> class shared_ptr;

share_ptr::share_ptr

  1. constexpr shared_ptr() noexcept;
  2. constexpr shared_ptr(nullptr_t) : shared_ptr() {}
  3. template <class U> explicit shared_ptr (U* p);
  1. template <class U, class D> shared_ptr (U* p, D del);
  2. template <class D> shared_ptr (nullptr_t p, D del);
  1. // shared_ptr constructor example
  2. #include <iostream>
  3. #include <memory>
  4. struct C {int* data;};
  5. int main () {
  6. std::shared_ptr<int> p1;
  7. std::shared_ptr<int> p2 (nullptr);
  8. std::shared_ptr<int> p3 (new int);
  9. std::shared_ptr<int> p4 (new int, std::default_delete<int>());
  10. std::shared_ptr<int> p5 (new int, [](int* p){delete p;}, std::allocator<int>());
  11. std::shared_ptr<int> p6 (p5);
  12. std::shared_ptr<int> p7 (std::move(p6));
  13. std::shared_ptr<int> p8 (std::unique_ptr<int>(new int));
  14. std::shared_ptr<C> obj (new C);
  15. std::shared_ptr<int> p9 (obj, obj->data);
  16. std::cout << "use_count:\n";
  17. std::cout << "p1: " << p1.use_count() << '\n';
  18. std::cout << "p2: " << p2.use_count() << '\n';
  19. std::cout << "p3: " << p3.use_count() << '\n';
  20. std::cout << "p4: " << p4.use_count() << '\n';
  21. std::cout << "p5: " << p5.use_count() << '\n';
  22. std::cout << "p6: " << p6.use_count() << '\n';
  23. std::cout << "p7: " << p7.use_count() << '\n';
  24. std::cout << "p8: " << p8.use_count() << '\n';
  25. std::cout << "p9: " << p9.use_count() << '\n';
  26. return 0;
  27. }

shared_ptr::use_count

  1. 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).

weak_ptr

  1. template <class T> class weak_ptr;

指向共享资源的指针,但是不增加引用计数,用于监察shared_ptr状态

weak_ptr::weak_ptr

  1. constexpr weak_ptr() noexcept;
  2. weak_ptr (const weak_ptr& x) noexcept;
  3. template <class U>
  4. weak_ptr (const weak_ptr<U>& x) noexcept;
  5. template <class U>
  6. weak_ptr (const shared_ptr<U>& x) noexcept;

weak_ptr::use_count

  1. #include <iostream>
  2. #include <memory>
  3. int main () {
  4. std::shared_ptr<int> sp (new int);
  5. std::weak_ptr<int> wp1;
  6. std::weak_ptr<int> wp2 (wp1);
  7. std::weak_ptr<int> wp3 (sp);
  8. std::cout << "use_count:\n";
  9. std::cout << "wp1: " << wp1.use_count() << '\n';
  10. std::cout << "wp2: " << wp2.use_count() << '\n';
  11. std::cout << "wp3: " << wp3.use_count() << '\n';
  12. return 0;
  13. }

weak_ptr::reset

  1. void reset() noexcept;
  2. //The object becomes empty, as if default constructed.

weak_ptr::lock

  1. 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.

weak_ptr::bool expired

  1. 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.

make_shared

  1. template <class T, class... Args>
  2. 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).

  1. args
  2. List of elements passed to T's constructor.
  3. Args is a list of zero or more types.

example

  1. #include <iostream>
  2. #include <memory>
  3. int main () {
  4. std::shared_ptr<int> foo = std::make_shared<int> (10);
  5. // same as:
  6. std::shared_ptr<int> foo2 (new int(10));
  7. auto bar = std::make_shared<int> (20);
  8. auto baz = std::make_shared<std::pair<int,int>> (30,40);
  9. std::cout << "*foo: " << *foo << '\n';
  10. std::cout << "*bar: " << *bar << '\n';
  11. std::cout << "*baz: " << baz->first << ' ' << baz->second << '\n';
  12. return 0;
  13. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注