Serving the Quantitative Finance Community

 
User avatar
tags
Topic Author
Posts: 3602
Joined: February 21st, 2010, 12:58 pm

std::shared_ptr VS boost::shared_ptr

February 28th, 2013, 8:46 am

Hi everyone.I got a bit (say) confused as I discovered it exists std::shared_ptr (it was released with C++11 if I understand well).What are the main differences between std::shared_ptr and boost::shared_ptr?It is right I can google, but I would like to know which one you recommend over the other? (and why?)MerciÉdouard
 
User avatar
BrightDay
Posts: 1
Joined: August 14th, 2003, 12:25 pm

std::shared_ptr VS boost::shared_ptr

February 28th, 2013, 8:59 am

The one from boost is the predecessor of the one from std. You can use either of them and their behaviour and performance should be comparable.In my case, I think I will be moving towards std::shared_ptr in the long term, but since we have a lots of libraries that are compiled under different architectures and with different versions of the compiler it will take many years (if ever) to replace boost::shared_ptr,Main advantage of boost::shared_ptr<> is that it will compile with older compilers. If you are starting a new application from scratch and you don't have legacy issues then I would suggest to use std::shared_ptr instead of boost::shared_ptr, otherwise the one from boost may be the only available.Either way it won't make much difference.
 
User avatar
Cuchulainn
Posts: 22929
Joined: July 16th, 2004, 7:38 am

std::shared_ptr VS boost::shared_ptr

February 28th, 2013, 11:36 am

Boost also has scoped_ptr, intrusive_ptr, scoped_array, shared_array and weak ptrs. (std only has shared_ptr and weak_ptr).
Last edited by Cuchulainn on February 27th, 2013, 11:00 pm, edited 1 time in total.
 
User avatar
CluelessCpp
Posts: 0
Joined: April 7th, 2012, 11:45 am

std::shared_ptr VS boost::shared_ptr

February 28th, 2013, 12:15 pm

std also has a unique_ptr.BTW, the _array variants are not needed as unique_ptr and shared_ptr can handle arrays as well.
 
User avatar
quantstart
Posts: 0
Joined: March 7th, 2010, 11:16 am

std::shared_ptr VS boost::shared_ptr

February 28th, 2013, 12:38 pm

This really comes down to your compiler environment. If you have a clean slate, then use C++11 and std::shared_ptr. If there is a chance of any legacy compilation then just use boost::shared_ptr.
 
User avatar
Cuchulainn
Posts: 22929
Joined: July 16th, 2004, 7:38 am

std::shared_ptr VS boost::shared_ptr

February 28th, 2013, 12:55 pm

QuoteOriginally posted by: CluelessCppBTW, the _array variants are not needed [...]Unless you using raw legacy C arrays?
Last edited by Cuchulainn on February 27th, 2013, 11:00 pm, edited 1 time in total.
 
User avatar
CluelessCpp
Posts: 0
Joined: April 7th, 2012, 11:45 am

std::shared_ptr VS boost::shared_ptr

March 1st, 2013, 8:09 am

QuoteOriginally posted by: CuchulainnQuoteOriginally posted by: CluelessCppBTW, the _array variants are not needed [...]Unless you using raw legacy C arrays?Sorry, should have put that into the context of the std smart pointers - these are specialised for array types to call the correct delete operator for arrays, e.g. std::unique_ptr<int[]> ptr (new int[10]);works fine, in contrast to std::unique_ptr<int> ptr (new int[10]);which compiles, but would use the non-array delete operator.So there is no reason to have a std::unique_array.
 
User avatar
Cuchulainn
Posts: 22929
Joined: July 16th, 2004, 7:38 am

std::shared_ptr VS boost::shared_ptr

March 1st, 2013, 8:21 am

QuoteOriginally posted by: CluelessCppQuoteOriginally posted by: CuchulainnQuoteOriginally posted by: CluelessCppBTW, the _array variants are not needed [...]Unless you using raw legacy C arrays?Sorry, should have put that into the context of the std smart pointers - these are specialised for array types to call the correct delete operator for arrays, e.g. std::unique_ptr<int[]> ptr (new int[10]);works fine, in contrast to std::unique_ptr<int> ptr (new int[10]);which compiles, but would use the non-array delete operator.So there is no reason to have a std::unique_array.Nice.I must say boost::scoped_ptr is good to use in code for short-lived objects (e.g. factories) and the reader can immediately see that it is not used outside the scope. shared_ptr is also fine but is a bit more relaxed.