this post was submitted on 30 Nov 2023
-1 points (45.5% liked)

C++

1779 readers
2 users here now

The center for all discussion and news regarding C++.

Rules

founded 1 year ago
MODERATORS
-1
The Myth of Smart Pointers (www.logikalsolutions.com)
submitted 1 year ago by lysdexic to c/cpp
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 6 points 1 year ago* (last edited 1 year ago) (1 children)

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 1 points 1 year 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.