what's the point of std::unique_ptr::get

lezebulon picture lezebulon · May 29, 2012 · Viewed 38.3k times · Source

Doesn't std::unique_ptr::get defeat the purpose of having a unique_ptr in the first place? I would have expected this function to change its state so it holds no more pointer. Is there an actual useful use of std::unique_ptr::get?

Answer

bames53 picture bames53 · May 29, 2012

std::unique_ptr provides unique ownership semantics safely. However that doesn't rule out the need for non-owning pointers. std::shared_ptr has a non-owning counterpart, std::weak_ptr. Raw pointers operate as std::unique_ptr's non-owning counterpart.