3 ms·
> Used to be foo* x=new foo(). Now it's std::shared_ptr<foo> x=std::make_shared<foo>() There's `auto` for that reason. No need to duplicate the info. And: In
by johannes1234321 4y ago
> Used to be foo* x=new foo(). Now it's std::shared_ptr<foo> x=std::make_shared<foo>()
There's `auto` for that reason. No need to duplicate the info.
And: In general code is read more often than written, and the variant with `make_shared` provides extra information on the purpose of the heap allocation. That first line with the raw pointer and new requires following all calls closely to understand the intention. (Aside from also handling the lifecycle, which would this be code which one would have to write elsewhere, your example is missing at least one `delete x` (and likely more handling, considering this being a shared, not unique, ptr)