Hacker News new | ask | show | jobs
by vinkelhake 3004 days ago
I realize that you're mostly ranting, but on the off chance that you're serious: check out std::make_shared. It's a helper function that can allocate storage for the object and shared_ptr's control block in one go. It comes with its own caveats (what doesn't in C++?), but it has the answer to your concern about allocations.
2 comments

To add: according to cppreference, all known STL implementations leverage this optimization. This used to be specific to Microsoft's STL implementation, but apparently all the major STL vendors are now on-board with it.
make_shared traces its roots back into the original boost implementation.
Yep I know it :)

My joke was "Oh, I should return a unique_ptr<>, but then I will be losing out on the make_shared optimization" ;)