abseil / Tip of the Week #187 : std::unique_ptr Must Be Moved

/187

  • Tip of the Week #187: std::unique_ptr Must Be Moved
    https://abseil.io/tips/187

    Originally posted as TotW #187 on November 5, 2020

    By Andy Soffer

    Updated 2020-11-05

    Quicklink: abseil.io/tips/187

    If you say in the first chapter that there is a std::unique_ptr on the wall, in the second or third chapter it absolutely must be moved. If it’s not going to be moved, it shouldn’t be hanging there. ~ With apologies to Anton Chekhov

    A std::unique_ptr is used for expressing transfer of ownership. If you never pass ownership elsewhere, the std::unique_ptr abstraction is rarely necessary or appropriate.

    What is a std::unique_ptr?

    A std::unique_ptr is a pointer that automatically destroys whatever it is pointing at when the std::unique_ptr itself is destroyed. It exists to convey ownership (the responsibility to destroy resources) as part of the type system and is one of (...)