开发者

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

开发者 https://www.devze.com 2023-01-02 18:31 出处:网络
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&

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 t开发者_C百科he 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()


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.


Note that other "shareholders" will be very surprised, to say the least, if a shared const T suddenly changes...

0

精彩评论

暂无评论...
验证码 换一张
取 消