[shared_ptr] towards a static_cast applied to smart_ptr

Hello Peter, the following simplified example do not compile when DONT_NOT_COMPILE is defined. #include <boost/shared_ptr.hpp> using namespace boost; struct X { int i; }; int main () { shared_ptr<X> ptrX1(new X()); shared_ptr<void> ptr_void(ptrX1); #if DONT_NOT_COMPILE shared_ptr<X> ptrX2(ptr_void); #endif return 0; } test_thread_shared_ptr.cpp: In function `int main()': test_thread_shared_ptr.cpp:37: error: no matching function for call to `boost::s hared_ptr<X>::shared_ptr(boost::shared_ptr<void>&)' ../../../boost/shared_ptr.hpp:154: note: candidates are: boost::shared_ptr<X>::s hared_ptr(const boost::shared_ptr<X>&) ../../../boost/shared_ptr.hpp:241: note: boost::shared_ptr< <template-parameter -1-1> >::shared_ptr(const boost::detail::shared_count&, T*) [with T = X] ../../../boost/shared_ptr.hpp:167: note: boost::shared_ptr< <template-parameter -1-1> >::shared_ptr() [with T = X] This is normal as void* is not convertibel to X*. But in my program I can ensure that the pointeed object by ptr_void is a X pointer. So it would be safe to do a kind of static_cast on shared_ptr. At the end smart pointers should behave like pointers. My real use case is a map of void* to shared_ptr<void>. I insert pairs of T* and shared_ptr<T> and I want be able to static_cast the resulting shared_ptr<void> of find function to shared_ptr<T>. Can I achieve this already? I was thinking is something like something like shared_ptr<X> ptrX2(boost::static_cast<shared_ptr<X> >(ptr_void)); If we can not achieve this now, I think that it should be not too dificult to implement inside your library. Do you think that this is an interesting feature to be included in the Boost.SmartPtr library? Best, Vicente Botet

On Thu, Oct 2, 2008 at 10:41 PM, vicente.botet <vicente.botet@wanadoo.fr> wrote:
This is normal as void* is not convertibel to X*. But in my program I can ensure that the pointeed object by ptr_void is a X pointer. So it would be safe to do a kind of static_cast on shared_ptr. At the end smart pointers should behave like pointers.
Can I achieve this already? I was thinking is something like something like
shared_ptr<X> ptrX2(boost::static_cast<shared_ptr<X> >(ptr_void));
Use boost::static_pointer_cast. You can also use the aliasing constructor to do tricks like make a shared_ptr point to a subobject of the pointee, for example you can make a shared_ptr<float> from a shared_ptr<std::pair<int,float> >. Emil Dotchevski Reverge Studios, Inc. http://www.revergestudios.com/reblog/index.php?n=ReCode

----- Original Message ----- From: "Emil Dotchevski" <emil@revergestudios.com> To: <boost@lists.boost.org> Sent: Friday, October 03, 2008 8:07 AM Subject: Re: [boost] [shared_ptr] towards a static_cast applied to smart_ptr
On Thu, Oct 2, 2008 at 10:41 PM, vicente.botet <vicente.botet@wanadoo.fr> wrote:
This is normal as void* is not convertibel to X*. But in my program I can ensure that the pointeed object by ptr_void is a X pointer. So it would be safe to do a kind of static_cast on shared_ptr. At the end smart pointers should behave like pointers.
Can I achieve this already? I was thinking is something like something like
shared_ptr<X> ptrX2(boost::static_cast<shared_ptr<X> >(ptr_void));
Use boost::static_pointer_cast. You can also use the aliasing constructor to do tricks like make a shared_ptr point to a subobject of the pointee, for example you can make a shared_ptr<float> from a shared_ptr<std::pair<int,float> >.
Thanks Emil for pointing me to static_pointer_cast. I understand now the use of aliasing. Thanks again, Vicente
participants (2)
-
Emil Dotchevski
-
vicente.botet