Hello!

Is the following use of reinterpret_cast safe:

void f(void*& p);

void g(int* p)
{
f(reinterpret_cast<void*&>(p));
}

If my memory serves me right, the standard says that the convertion from T*
to void* and back to T* should yield the same pointer value, but I do not
remember anything about references to pointers.

Re: Is conversion from T* to void*& safe? by Igor

Igor
Wed Apr 16 16:30:13 CDT 2008

Angel Tsankov <fn42551@fmi.uni-sofia.bg> wrote:
> Is the following use of reinterpret_cast safe:
>
> void f(void*& p);
>
> void g(int* p)
> {
> f(reinterpret_cast<void*&>(p));
> }
>
> If my memory serves me right, the standard says that the convertion
> from T* to void* and back to T* should yield the same pointer value,
> but I do not remember anything about references to pointers.

Your code is essentially equivalent to

f(*reinterpret_cast<void**>(&p));

The behavior is implementation-defined. There is no special case for
this in reinterpret_cast description.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: Is conversion from T* to void*& safe? by Doug

Doug
Wed Apr 16 16:39:14 CDT 2008

On Thu, 17 Apr 2008 00:22:37 +0300, "Angel Tsankov"
<fn42551@fmi.uni-sofia.bg> wrote:

>Hello!
>
>Is the following use of reinterpret_cast safe:
>
>void f(void*& p);
>
>void g(int* p)
>{
> f(reinterpret_cast<void*&>(p));
>}
>
>If my memory serves me right, the standard says that the convertion from T*
>to void* and back to T* should yield the same pointer value, but I do not
>remember anything about references to pointers.

I'm unaware of any compiler for which it will fail to do the expected
thing, but it's still dangerous, because f could be:

void f(void*& p)
{
static double x;
p = &x;
}

--
Doug Harrison
Visual C++ MVP