4 ms·
With std::make_shared, and the addition of std::make_unique in C++14, you shouldn't ever need to use new for this purpose either.
by hendzen 12y ago
With std::make_shared, and the addition of std::make_unique in C++14, you shouldn't ever need to use new for this purpose either.
- Suncho 12y agoIt depends. The trick to using new correctly is to put it in a function whose sole purpose is to immediately store new's result in the smart handle. I.E. You're writing a function that's analogous to the make_unique function. You might need to do this if you're implementing your own smart handle or writing a factory function for a class with a private constructor (make_unique will not work). Off the top of my head, I can't think of any good reasons to use delete.
- lomnakkus 12y agoIf your handles are pointer types, you should still use unique_ptr, you just need to give it a custom "deleter". (Of course you should probably still encapsulate this in a function like make_xxx() just to avoid the potential mistake of not supplying the correct deleter everywhere.)