Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost::shared_ptr<const T> to boost::shared_ptr<T>

I want to cast the const-ness out of a boost::shared_ptr, but I boost::const_pointer_cast is not the answer. boost::const_pointer_cast wants a const boost::shared_ptr<T>, not a boost::shared_ptr<const T>. Let's forego the obligatory "you shouldn't be doing that". I know... but I need to do it... so what's the best/easiest way to do it?

For clarity sake:

boost::shared_ptr<const T> orig_ptr( new T() );

boost::shared_ptr<T> new_ptr = magic_incantation(orig_ptr);

I need to know the magic_incantation()

like image 549
Flevine Avatar asked Dec 23 '22 02:12

Flevine


1 Answers

boost::const_pointer_cast is the function you want to use:

boost::shared_ptr<const int> ci(new int(42));
boost::shared_ptr<int> i(boost::const_pointer_cast<int>(ci));

Does that not work for you? I tested that with both Boost 1.43 and the Visual C++2010 C++0x implementation--no issues with either.

like image 172
James McNellis Avatar answered Dec 31 '22 22:12

James McNellis