On 2014/03/28 12:16:14, Sven Panne wrote:
On 2014/03/28 11:59:58, jbramley wrote:
> [...] However, it does have a rather surprising note on
> reinterpret_cast suggesting that reinterpret_cast<T&>(x) can be used to
safely
> do type-punning. I'd not seen this before, and every resource I've seen
until
> now has said that this is _not_ safe. This (draft) standard is dated
2005-10-19
> and I'm not sure where I got it from.
Hmmm interesting, I didn't know that. I have a Working Draft
(N3242=11-0012)
from 2011-02-28 which says:
----------------------------------------------
5.2.10 Reinterpret cast
... 11 An lvalue expression of type T1 can be cast to the type "reference
to
T2"
if an expression of type "pointer to T1" can be explicitly converted to
the
type
"pointer to T2" using a reinterpret_cast. That is, a reference cast
reinterpret_cast<T&>(x) has the same effect as the conversion
*reinterpret_cast<T*>(&x) with the built-in & and * operators (and
similarly
for
reinterpret_cast<T&&>(x)). The result refers to the same object as the
source
lvalue, but with a different type. The result is an lvalue for an lvalue
reference type or an rvalue reference to function type and an xvalue for
an
rvalue reference to object type. No temporary is created, no copy is
made, and
constructors (12.1) or conversion functions (12.3) are not called. (71)
(This
is
sometimes referred to as a type pun.)
----------------------------------------------
That's very similar to mine (N1905=05-0165).
> I still think that the only _portable_ way to do it is to use memcpy;
that's
> safe in every version of the standards, but the rules are rather more
relaxed
> than I previously thought.
One can read the section above in the sense that a reference cast is safe
for
our purposes, too, but the next question is: Do all compilers implement
that
correctly? :-}
That is an good question, especially if a C++ compiler is based on C
behaviour,
where such casts are not safe.
https://codereview.chromium.org/213943002/
--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.