简体   繁体   English

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

[英]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::shared_ptr抛出const-ness,但是我的boost::const_pointer_cast不是答案。 boost::const_pointer_cast wants a const boost::shared_ptr<T> , not a boost::shared_ptr<const T> . boost::const_pointer_cast想要一个const boost::shared_ptr<T> ,而不是一个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() 我需要知道magic_incantation()

boost::const_pointer_cast is the function you want to use: boost::const_pointer_cast 你想要使用的函数:

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. 我使用Boost 1.43和Visual C ++ 2010 C ++ 0x实现进行了测试 - 两者都没有问题。

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

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

相关问题 做boost :: shared_ptr <T> 和boost :: shared_ptr <const T> 分享参考计数? - Do boost::shared_ptr<T> and boost::shared_ptr<const T> share the reference count? 从boost :: shared_ptr转换而来 <T> to boost :: shared_ptr <const T> - convert from boost::shared_ptr<T> to boost::shared_ptr<const T> 转换 const std::shared_ptr<const t> 进入 boost::shared_ptr<t></t></const> - convert const std::shared_ptr<const T> into boost::shared_ptr<T> 如何将boost :: atomic_store与shared_ptr一起使用 <T> 和shared_ptr <const T> ? - How to use boost::atomic_store with shared_ptr<T> and shared_ptr<const T>? 转换(隐式)提升shared_ptr <T> 到shared_ptr <const T> - Converting (implicit) boost shared_ptr<T> to shared_ptr<const T> 什么是boost的shared_ptr(shared_ptr <Y> const&r,T * p)用于? - What is boost's shared_ptr(shared_ptr<Y> const & r, T * p) used for? const boost :: shared_ptr的目的 <T> &作为函数参数? - Purpose of const boost::shared_ptr<T>& as function argument? 使用Boost Python与shared_ptr <const T> - Using Boost Python with shared_ptr<const T> 一个神秘的编译错误:无法从&#39;const boost :: shared_ptr转换 <T> &#39;to&#39;const boost :: shared_ptr <T> “ - A mysterious compilation error: cannot convert from 'const boost::shared_ptr<T>' to 'const boost::shared_ptr<T>' 提升shared_ptr和&#39;this&#39; - boost shared_ptr and 'this'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM