Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Casts

23 views
Skip to first unread message

Paavo Helde

unread,
Sep 21, 2018, 12:09:03 PM9/21/18
to
On 21.09.2018 18:26, Stefan Ram wrote:
> I wonder whether there is any difference (in value/behavior)
> between the following two expressions, and which one should
> be used to convert a pointer to an unsigned integer type in
> the sense of
>
> N2176 (C, not C++!), 7.20.1.4p1
>
> |The following type designates an unsigned integer type with
> |the property that any valid pointer to void can be converted
> |to this type, then converted back to pointer to void, and the
> |result will compare equal to the original pointer:
> |
> |uintptr_t
>
> . Here are the two expressions referred to by the first
> sentence of this post:
>
> reinterpret_cast< ::std::uintptr_t >pointer
>
> static_cast< ::std::uintptr_t >pointer

As the second one does not compile, the choice seems easy to me ;-)

It does not compile because despite its name uintptr_t is not a pointer
type.


Alf P. Steinbach

unread,
Sep 21, 2018, 3:43:33 PM9/21/18
to
On 21.09.2018 17:26, Stefan Ram wrote:
> [snip]
> reinterpret_cast< ::std::uintptr_t >pointer
> static_cast< ::std::uintptr_t >pointer

Both are syntax errors.

When that's fixed the `static_cast` is invalid.

* * *

There was once an issue with the standard's wording for
`reinterpret_cast`, so that Andrei and Herb recommended

static_cast<Ptr_to_A>( static_cast<void*>( ptr_to_B ) )

instead of

reinterpret_cast<Ptr_to_A>( ptr_to_B ) )

However, as I recall, but this is a vague memory, the wording was fixed
in C++11 so that now the two are also formally equivalent, so no longer
is there even a formal reason to use the more convoluted construct.


Cheers!

- Alf
0 new messages