Boost logo

Boost Users :

Subject: Re: [Boost-users] [smart_ptr] Reinterpret_pointer_cast()?
From: Richard Damon (Richard_at_[hidden])
Date: 2011-11-14 11:04:54


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

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net