3 ms·
The only difference being that in the case of a smart pointer, you can't ignore the possibility of a null by accident.
by tomsmeding 9y ago
The only difference being that in the case of a smart pointer, you can't ignore the possibility of a null by accident.
- civility 9y agoYou can easily ignore null by accident while using smart pointers: struct foo { int bar; }; std::unique_ptr<foo> bug; bug->bar = 123;
- tomsmeding 9y agoTrue, didn't think of that. Then indeed they are quite useless, and IIRC they are actuallt deprecated in C++17.
- civility 9y agoI don't agree they are useless - they don't stop you from accessing nullptrs, but they do help manage the lifetimes of your dynamically created objects. And I haven't seen anything indicating unique_ptr is to be deprecated - maybe you're thinking about auto_ptr.
- albinofrenchy 9y agoSmart pointers are not at all useless and not at all deprecated (you are thinking of auto_ptr; there are still unique_ptr and shared_ptr). They don't try to address the check for null problem at all; that isn't their intent. All they mean to do is eliminate several classes of common, easy to make mistakes such as not zero initializing or not freeing the resource.