[smart_ptr] Reinterpret_pointer_cast()?

Even as I ask this I suspect the answer is obvious and I can't see it, but is there any reason why there is no reinterpret_pointer_cast() function for shared_ptr's? Thx, - Rob.

Den 14-11-2011 14:11, Robert Jones skrev:
Even as I ask this I suspect the answer is obvious and I can't see it, but is there any reason why there is no reinterpret_pointer_cast() function for shared_ptr's?
Well, its almost never needed. static_pointer_cast should do the trick. -Thorsten

On 11/14/11 8:11 AM, Robert Jones wrote:
Even as I ask this I suspect the answer is obvious and I can't see it, but is there any reason why there is no reinterpret_pointer_cast() function for shared_ptr's?
Thx,
- Rob.
I would say that I can't think of a case where a reinterpret_cast on what is held by a smart pointer would be correct (unless you are badly abusing the smart pointer). Since the pointer will be eventually deleted by the shared pointer, the type of the pointer really needs to be that, or a base, of the type of the real object. At that point a static/dynamic_cast would be the appropriate action, not a reinterpret_cast. In general, reinterpret_casts take you into the realm of implementaion/un-defined behavior (with a very few exceptions). If you really need to do it, forcing you to extract the pointer value and doing the cast explicitly as a reinterpret_cast isn't that high of a cost, and doesn't add another pattern to search the code for to detect questionable code. -- Richard Damon
participants (3)
-
Richard Damon
-
Robert Jones
-
Thorsten Ottosen