Search code examples
c++c++14smart-pointersunique-ptrweak-ptr

Does weak_ptr work alongside unique_ptr?


As I understand it, a std::weak_ptr is used as a safe way of referencing memory referenced by a std::shared_ptr which may have been deallocated. Can it be used with a std::unique_ptr as well?


Solution

  • As I understand it std::weak_ptr is used as a safe way of referencing memory referenced by shared_ptrs which may have been deallocated.

    You understand wrong. std::weak_ptr allowes to access object, which ownership is maintained by std::shared_ptr without sharing it. Now when you really understand what std::weak_ptr does you should understand that your question about std::unique_ptr does not make any sense.