• vzq@lemmy.blahaj.zone
    link
    fedilink
    arrow-up
    6
    ·
    edit-2
    11 months ago

    I’ve been using unique_ptr since way before it was called that, and it’s not something magical. You need to know what it does and what it doesn’t or you’ll be surprised.

    I always thought of unique as a warning not a guarantee. Make sure it’s the only thing pointing at your structure, or you’ll be in trouble.

    • lysdexic@programming.devOPM
      link
      fedilink
      English
      arrow-up
      1
      ·
      11 months ago

      You need to know what it does and what it doesn’t or you’ll be surprised.

      I don’t think that std::unique_ptr is shrouded in mystery: it’s designed to be the unique handle of a raw pointer, and it frees the memory when it’s lifetime ends. As it’s designed to be the unique holder of a resource, it’s implemented to disallow making copies. It might be implemented in clever ways, but a developer experience point of view it’s quite straight to the point.