4 ms·
> Garbage collection does almost the same thing As far as I understand, smart pointers are actually a somewhat stupid form of garbage collection.
by nsajko 6y ago
> Garbage collection does almost the same thing
As far as I understand, smart pointers are actually a somewhat stupid form of garbage collection.
- ncmncm 6y agoWe usually prefer the adjective "deterministic" over "stupid". Simplicity, zero overhead, and "user pays" are virtues. People who complain about C++ smart pointers tend to be the ones who reflexively reach for shared_ptr. (We call that habit "Java Disease".) Most good C++ programs have zero uses of shared_ptr. Also, passing around shared_ptr or unique_ptr in public interfaces puts you in a state of sin.
- Aeolos 6y ago> Also, passing around shared_ptr or unique_ptr in public interfaces puts you in a state of sin. Why is that?
- ncmncm 6y agoIf they are passed by value or move, you are doing complicated things with ownership that deserve to be encapsulated in a named type. If you are passing them by const reference, you would usually better pass a regular pointer or reference to the pointed-to type. If you are passing them by non-const reference, you have bigger problems. In practice, the new type usually starts out as a PIMPL class with just a std::unique_ptr<impl> member. But often, once the type is named, you discover it is a good place to park more operations.