Boost :: shared_ptr <const T> for boost :: shared_ptr <T>

I want to infer const-ness from boost::shared_ptr , but I boost::const_pointer_cast not the answer. boost::const_pointer_cast wants a const boost::shared_ptr<T> , not boost::shared_ptr<const T> . Let me give up the obligatory "you must not do this." I know ... but I need to do this ... so what's the best / easiest way to do this?

For clarity:

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

I need to know magic_incantation ()

+4
source share
2 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)); 

Doesn't this work for you? I tested this with both Boost 1.43 and Visual C ++ 2010 C ++ 0x - no problem with them.

+9
source

Please note that other โ€œshareholdersโ€ will be very surprised if not to say that the total const T will suddenly change ...

+2
source

Source: https://habr.com/ru/post/1312386/


All Articles