[Boost users] [interprocess]: static_pointer_cast on offset_ptr

Hi, In offset_ptr.hpp the static_pointer_cast<> &co are commented out. Why is this? I need something of the kind to do casts from a void* to a bool*, for example. Is there something I am missing? Lars

You can theoretically use reinterpret_cast to do that. However, you should be warned that void * hacks are extremely dangerous, and in poor taste. You should be better off, and much much safer, using templates instead. The static pointer cast may be commented out for that very reason :) mike
In offset_ptr.hpp the static_pointer_cast<> &co are commented out. Why is this? I need something of the kind to do casts from a void* to a bool*, for example.
Is there something I am missing?
Lars
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Yes, but it has to be safer and more correct to use static_pointer_casts instead of reinterpret_cast. In my particular situation I am unfortunately not able to use templates, so I do need to do "ugly" pointer stuff. (I am porting an existing shared-memory solution to boost::interprocess). I've uncommented the static_pointer_cast et al functions from offset_ptr.hpp, and they appear to do their work beautifully. Lars Michael Linck wrote:
You can theoretically use reinterpret_cast to do that. However, you should be warned that void * hacks are extremely dangerous, and in poor taste. You should be better off, and much much safer, using templates instead.
The static pointer cast may be commented out for that very reason :)
mike
In offset_ptr.hpp the static_pointer_cast<> &co are commented out. Why is this? I need something of the kind to do casts from a void* to a bool*, for example.
Is there something I am missing?
Lars
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Lars Hagström escribió:
Yes, but it has to be safer and more correct to use static_pointer_casts instead of reinterpret_cast. In my particular situation I am unfortunately not able to use templates, so I do need to do "ugly" pointer stuff. (I am porting an existing shared-memory solution to boost::interprocess).
I've uncommented the static_pointer_cast et al functions from offset_ptr.hpp, and they appear to do their work beautifully.
Lars
Sorry for the late reply. Originally I wrote static_pointer_cast & co.
functions to mimic shared_ptr. I even used them as generic casting
function for code where the pointer type is templated.
However, those functions need an explicit template argument so ADL was
not activated (say, like when you use an unqualified swap call). In the
end I decided to comment them to prevent users using them (I should have
erased them...).
You can achieve the same using:
offset_ptr(static_cast

Hi, No, I don't really mind using the "long way". It is just that I was surprised that the casts were commented out, since I kinda liked the way that they mirrored shared_ptrs casting functions. Using those casts are slightly more legible than doing it the long way. But if you feel that they don't belong in the library, then I accept your judgement. Lars Ion Gaztañaga wrote:
Lars Hagström escribió:
Yes, but it has to be safer and more correct to use static_pointer_casts instead of reinterpret_cast. In my particular situation I am unfortunately not able to use templates, so I do need to do "ugly" pointer stuff. (I am porting an existing shared-memory solution to boost::interprocess).
I've uncommented the static_pointer_cast et al functions from offset_ptr.hpp, and they appear to do their work beautifully.
Lars
Sorry for the late reply. Originally I wrote static_pointer_cast & co. functions to mimic shared_ptr. I even used them as generic casting function for code where the pointer type is templated.
However, those functions need an explicit template argument so ADL was not activated (say, like when you use an unqualified swap call). In the end I decided to comment them to prevent users using them (I should have erased them...).
You can achieve the same using:
offset_ptr(static_cast
(my_offset_ptr.get())); without any performance hit, so I didn't consider them essential (ok, you have to type more ;-)). Is there any reason why you prefer static_pointer_cast instead of the code above? I have no problem to uncomment the code if you find it useful.
Regards,
Ion _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Ion Gaztañaga
-
Lars Hagström
-
Michael Linck