Cast boost :: shared_array <char> for boost :: shared_array <const char>

How do I drop boost::shared_array<char> in boost::shared_array<const char> ?

+6
c ++ casting boost smart-pointers
source share
7 answers

Since shared_array does not have an add_ref method, you can emulate it as follows:

 struct MagicDeleter { MagicDeleter( boost::shared_array<char> ptr ) : ptr(ptr) {}; template<typename T> void operator()(T*) {} protected: boost::shared_array<char> ptr; }; ... boost::shared_array<char> orig_ptr( some_val ); boost::shared_array<const char> new_ptr( orig_ptr.get(), MagicDeleter(orig_ptr) ); 
+6
source share

Other answers are correct, you cannot and should not.

Also, are you sure you want boost::shared_array<const char> and not const boost::shared_array<char> ?

Practically this works:

 boost::shared_array<char> acz; boost::shared_array<const char>& acz2 = reinterpret_cast< boost::shared_array<const char>& >(acz); 

BUT this is not a good idea and works only if boost :: shared_array and boost :: shared_array have the same implementation. Templates can be partially specialized:

 template<class T> struct TwoImplementations { int m_nIntMember; }; template<> struct TwoImplementations< const T > { double m_fDoubleMember; }; 

Performing a reinterpretation between TwoImplementations<int> and TwoImplementations<const int> simply incorrect.

+3
source share

You can not.

Since both types are template-based, both types are completely different for the compiler.

+1
source share

I think you can’t. If you really need it, you can create your own class of smart pointers. Tips for this can be found here .

+1
source share

You can use the get () method to get the base char *, which is automatically converted to const char * - but does not assign it to another shared_array, because then you will delete the data twice. Just use it as you need it.

like this:

 boost::shared_array<char> x(new int[13]); const char *y = x.get(); 
+1
source share

I would not have thought of this without Kirill’s surprising answer, but you can effectively increase the boost static_pointer_cast , which is used for shared_ptr to work with shared_array as follows:

 template<typename OriginalType> struct SharedPtrCastHelper { public: SharedPtrCastHelper( const OriginalType & ptr ) : ptr(ptr) {}; template<typename T> void operator()(T*) {} protected: OriginalType ptr; }; template<typename OutT, typename InT> boost::shared_array<OutT> static_pointer_cast( const boost::shared_array<InT> & inSharedPtr ) { typedef SharedPtrCastHelper<boost::shared_array<InT> > Helper; return boost::shared_array<OutT>( (OutT*)inSharedPtr.get(), Helper(inSharedPtr) ); } 

with this you can do something like:

 boost::shared_array<int> intArrayPtr( new int[40] ); boost::shared_array<unsigned int> uintArrayPtr; uintArrayPtr = static_pointer_cast<unsigned int>( intArrayPtr ); 
+1
source share

Such generation using the compiler may not be possible.

The interiors of a class with a const-code template parameter can be very different from a class without one because of the function of a specialized template.

In addition, the use of such a function is sometimes the background for compile-time checks, which simply do not allow creating instances of type A<const T> for each case when type A<T> correct.

0
source share

All Articles