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

前端 未结 6 1959
半阙折子戏
半阙折子戏 2020-12-04 21:45

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

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-04 21:52

    Herb Sutter has a good explanation (around 3:40) https://www.youtube.com/watch?v=JfmTagWcqoE

    The main advantage is that the unique pointer keeps track of how many other references there are to that pointer. You only work with the unique pointer when you are working with ownership. When you want to do something with the data with that pointer, you pass the raw pointer.

提交回复
热议问题