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

Fle*_*ine 4 c++ boost casting const shared-ptr

我想把一个常数抛出来boost::shared_ptr,但我boost::const_pointer_cast不是答案. boost::const_pointer_cast想要一个const boost::shared_ptr<T>,而不是一个boost::shared_ptr<const T>.让我们放弃强制性的"你不应该这样做".我知道......但我需要这样做......那么最好/最简单的方法是什么?

为清楚起见:

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

boost::shared_ptr<T> new_ptr = magic_incantation(orig_ptr);
Run Code Online (Sandbox Code Playgroud)

我需要知道magic_incantation()

Jam*_*lis 9

boost::const_pointer_cast 你想要使用的功能:

boost::shared_ptr<const int> ci(new int(42));
boost::shared_ptr<int> i(boost::const_pointer_cast<int>(ci));
Run Code Online (Sandbox Code Playgroud)

这不适合你吗?我使用Boost 1.43和Visual C++ 2010 C++ 0x实现进行了测试 - 两者都没有问题.