繁体   English   中英

提高:: shared_ptr的 <const T> to boost :: shared_ptr <T>

[英]boost::shared_ptr<const T> to boost::shared_ptr<T>

我想从boost::shared_ptr抛出const-ness,但是我的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);

我需要知道magic_incantation()

boost::const_pointer_cast 你想要使用的函数:

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

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

请注意,至少可以说,如果共享的const T突然改变,其他“股东”会非常惊讶 ......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM