Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is boost::make_shared obsolete now?

Tags:

c++

boost

Is boost::make_shared obsolete now? Haven't found its definition in 1.35.

like image 224
Steve Avatar asked Nov 11 '09 02:11

Steve


3 Answers

Its in the 1.4 docs: http://www.boost.org/doc/libs/1_40_0/libs/smart_ptr/make_shared.html

It appears to have been added in version 1.39

like image 66
Ryan Cook Avatar answered Nov 16 '22 10:11

Ryan Cook


std::make_shared is also available in C++11. Please note that make_shared is more than just a convenience function. Take a look at the following code fragment:

make_shared<foobar>(1, 2);
shared_ptr<foobar>(new foobar(1, 2));

Both statements create a foobar object and construct a shared_ptr. However, the former avoids a memory allocation for the shared counter, because a single memory chunk will be used for the counter and the foobar object. This is not possible with the second statement, because the memory for foobar is allocated before the shared_ptr is constructed.

What I want to say: No, make_shared is not obsolete, because it provides a very useful optimization.

like image 42
nosid Avatar answered Nov 16 '22 10:11

nosid


Did a bit of research today, and it seems that make_shared actually was added to 1.36.0 (in 1.35.0 there is no such header), but the interesting thing is that there is no single mention in What's new about this change - at least I could not find it

like image 3
Alex Z Avatar answered Nov 16 '22 09:11

Alex Z