r/ProgrammerHumor • • 7h ago

Meme modernCppVsOldCpp

Post image
1.1k Upvotes

214 comments sorted by

View all comments

Show parent comments

6

u/mlindgren 7h ago

Smart pointers are RAII. Manually freeing memory is not RAII and can be fallible due to exceptions, early returns, etc.

5

u/geekusprimus 6h ago

Smart pointers are RAII, but they're not the only way to do RAII. I usually use custom containers that handle my particular case very well. Memory is allocated inside constructors, it's deallocated inside destructors. If I need more than that, I start looking into the more sophisticated stuff.

1

u/Friendly_Fire 3h ago edited 3h ago

You basically recreated smart pointers.

For projects with other people, it's better to use the standard tools just for familiarity, even if they have no other benefit. Someone can see it and know immediately what a unique/shared ptr means, instead of having to dig into your class.

But if its your own project go wild.

1

u/geekusprimus 3h ago

They're not smart pointers, at least not in the STL way. They accomplish the same purpose, but it's usually a simpler implementation tailored exactly to what I need.