Topic: reinterpret_cast on rvalues


Author: stackmachine@hotmail.com
Date: Mon, 18 Feb 2013 06:02:49 -0800 (PST)
Raw View
------=_Part_94_12708330.1361196169628
Content-Type: text/plain; charset=ISO-8859-1

I see no reason to disallow stuff like this:
auto x = reinterpret_cast<float>(42);
Currently one has to write stuff like this:
auto const x = rvalue;
auto y = *reinterpret_cast<float const*>(x);

Am I missing something?

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en.



------=_Part_94_12708330.1361196169628
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

I see no reason to disallow stuff like this:<br><div class=3D"prettyprint" =
style=3D"background-color: rgb(250, 250, 250); border-color: rgb(187, 187, =
187); border-style: solid; border-width: 1px; word-wrap: break-word;"><code=
 class=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"color: =
#008;" class=3D"styled-by-prettify">auto</span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify"> x </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-pr=
ettify">reinterpret_cast</span><span style=3D"color: #080;" class=3D"styled=
-by-prettify">&lt;float&gt;</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">(</span><span style=3D"color: #066;" class=3D"styled-by-pr=
ettify">42</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
);</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></sp=
an></div></code></div>Currently one has to write stuff like this:<br><div c=
lass=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); border-=
color: rgb(187, 187, 187); border-style: solid; border-width: 1px; word-wra=
p: break-word;"><code class=3D"prettyprint"><div class=3D"subprettyprint"><=
span style=3D"color: #008;" class=3D"styled-by-prettify">auto</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"c=
olor: #008;" class=3D"styled-by-prettify">const</span><span style=3D"color:=
 #000;" class=3D"styled-by-prettify"> x </span><span style=3D"color: #660;"=
 class=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> rvalue</span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"><br></span><span style=3D"color: #008;" class=3D"styled-by-pret=
tify">auto</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
 y </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">*</span><span style=3D"=
color: #008;" class=3D"styled-by-prettify">reinterpret_cast</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><span style=3D"=
color: #008;" class=3D"styled-by-prettify">float</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" =
class=3D"styled-by-prettify">const</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">*&gt;(</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify">x</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">);</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"><br></span></div></code></div><br>Am I missing something?<br>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_94_12708330.1361196169628--

.


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Mon, 18 Feb 2013 14:47:06 -0800 (PST)
Raw View
------=_Part_1078_2033501.1361227626620
Content-Type: text/plain; charset=ISO-8859-1

On Monday, February 18, 2013 6:02:49 AM UTC-8, stackm...@hotmail.com wrote:
>
> I see no reason to disallow stuff like this:
> auto x = reinterpret_cast<float>(42);
> Currently one has to write stuff like this:
> auto const x = rvalue;
> auto y = *reinterpret_cast<float const*>(x);
>
> Am I missing something?
>

Yes. That the latter is undefined behavior. In order for the specification
to specify the behavior of `reinterpret_cast<float>`, it has to actually
define what that means. And they don't want to, for many obvious reasons
(like allowing implementations the latitude to define integers however they
want, etc).

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en.



------=_Part_1078_2033501.1361227626620
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On Monday, February 18, 2013 6:02:49 AM UTC-8, stackm...@hotmail.com wrote:=
<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;bor=
der-left: 1px #ccc solid;padding-left: 1ex;">I see no reason to disallow st=
uff like this:<br><div style=3D"background-color:rgb(250,250,250);border-co=
lor:rgb(187,187,187);border-style:solid;border-width:1px;word-wrap:break-wo=
rd"><code><div><span style=3D"color:#008">auto</span><span style=3D"color:#=
000"> x </span><span style=3D"color:#660">=3D</span><span style=3D"color:#0=
00"> </span><span style=3D"color:#008">reinterpret_cast</span><span style=
=3D"color:#080">&lt;float&gt;</span><span style=3D"color:#660">(</span><spa=
n style=3D"color:#066">42</span><span style=3D"color:#660">);</span><span s=
tyle=3D"color:#000"><br></span></div></code></div>Currently one has to writ=
e stuff like this:<br><div style=3D"background-color:rgb(250,250,250);borde=
r-color:rgb(187,187,187);border-style:solid;border-width:1px;word-wrap:brea=
k-word"><code><div><span style=3D"color:#008">auto</span><span style=3D"col=
or:#000"> </span><span style=3D"color:#008">const</span><span style=3D"colo=
r:#000"> x </span><span style=3D"color:#660">=3D</span><span style=3D"color=
:#000"> rvalue</span><span style=3D"color:#660">;</span><span style=3D"colo=
r:#000"><br></span><span style=3D"color:#008">auto</span><span style=3D"col=
or:#000"> y </span><span style=3D"color:#660">=3D</span><span style=3D"colo=
r:#000"> </span><span style=3D"color:#660">*</span><span style=3D"color:#00=
8">reinterpret_cast</span><span style=3D"color:#660">&lt;</span><span style=
=3D"color:#008">float</span><span style=3D"color:#000"> </span><span style=
=3D"color:#008">const</span><span style=3D"color:#660">*&gt;(</span><span s=
tyle=3D"color:#000">x</span><span style=3D"color:#660">);</span><span style=
=3D"color:#000"><br></span></div></code></div><br>Am I missing something?<b=
r></blockquote><div><br>Yes. That the latter is undefined behavior. In orde=
r for the specification to specify the behavior of `reinterpret_cast&lt;flo=
at&gt;`, it has to actually define what that means. And they don't want to,=
 for many obvious reasons (like allowing implementations the latitude to de=
fine integers however they want, etc).<br></div>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_1078_2033501.1361227626620--

.


Author: Sebastian Gesemann <s.gesemann@gmail.com>
Date: Tue, 19 Feb 2013 13:54:30 +0100
Raw View
On Mon, Feb 18, 2013 at 3:02 PM,  <stackmachine@hotmail.com> wrote:
> I see no reason to disallow stuff like this:
>   auto x = reinterpret_cast<float>(42);
> Currently one has to write stuff like this:
>   auto const x = rvalue;
>   auto y = *reinterpret_cast<float const*>(x);

The last fragment invokes undefined behaviour (violated aliasing rule).

I guess you want reinterpret_cast<T>(expr) for a scalar type T to be
equivalent to

    template<class T, class U
        ,class = typename std::enable_if<
            std::is_scalar<T>::value &&
            std::is_scalar<U>::value &&
            sizeof(T)==sizeof(U)
        >::type
    >
    T reinterpret_value(U x)
    {
        T result;
        std::memcpy(&result,&x,sizeof result);
        return result;
    }

reinterpret_cast already does something like this but only for the
cases where T and U are both pointer types or one is a pointer type
and the other is an integer type. In addition, reinterpret_cast can
deal with references in the following way: reinterpret_cast<T&>(x) ===
*reinterpret_cast<T*>(&x). I guess you could argue that the
restriction to pointer and integer types is artificial and that there
is no good reason against generalizing this to scalar types like
above.

I'm not sure right now, though, whether reinterpret_cast cares for the
"size(T)==size(U)" constraint. The only requirement I can recall in
this respect is that a roundtrip from pointer to integer and back
again actually compiles, that you can be sure that the original
pointer value is restored. This would require an integer type that is
at least as large as the original pointer type. But I'd have to check
this again.

Cheers!
SG

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en.



.


Author: Johannes Schaub <schaub.johannes@googlemail.com>
Date: Wed, 20 Feb 2013 15:22:13 -0800 (PST)
Raw View
------=_Part_805_22212844.1361402533551
Content-Type: text/plain; charset=ISO-8859-1



On Monday, February 18, 2013 3:02:49 PM UTC+1, stackm...@hotmail.com wrote:
>
> I see no reason to disallow stuff like this:
> auto x = reinterpret_cast<float>(42);
> Currently one has to write stuff like this:
> auto const x = rvalue;
> auto y = *reinterpret_cast<float const*>(x);
>
>
That looks to me like a value cast of 42 to float, but would actually be a
reinterpreting cast. In the cases where you give a prvalue or cast to a
non-reference type, currently reinterpret_cast only does value casts, and
never directly reinterprets object representations with certain types.

So IMO this would be very confusing. You can do all that in one single line
anyway (and earn UB)

    auto y = reinterpret_cast<const float&>((const int&)42);

>
>

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en.



------=_Part_805_22212844.1361402533551
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<br><br>On Monday, February 18, 2013 3:02:49 PM UTC+1, stackm...@hotmail.co=
m wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0=
..8ex;border-left: 1px #ccc solid;padding-left: 1ex;">I see no reason to dis=
allow stuff like this:<br><div style=3D"background-color:rgb(250,250,250);b=
order-color:rgb(187,187,187);border-style:solid;border-width:1px;word-wrap:=
break-word"><code><div><span style=3D"color:#008">auto</span><span style=3D=
"color:#000"> x </span><span style=3D"color:#660">=3D</span><span style=3D"=
color:#000"> </span><span style=3D"color:#008">reinterpret_cast</span><span=
 style=3D"color:#080">&lt;float&gt;</span><span style=3D"color:#660">(</spa=
n><span style=3D"color:#066">42</span><span style=3D"color:#660">);</span><=
span style=3D"color:#000"><br></span></div></code></div>Currently one has t=
o write stuff like this:<br><div style=3D"background-color:rgb(250,250,250)=
;border-color:rgb(187,187,187);border-style:solid;border-width:1px;word-wra=
p:break-word"><code><div><span style=3D"color:#008">auto</span><span style=
=3D"color:#000"> </span><span style=3D"color:#008">const</span><span style=
=3D"color:#000"> x </span><span style=3D"color:#660">=3D</span><span style=
=3D"color:#000"> rvalue</span><span style=3D"color:#660">;</span><span styl=
e=3D"color:#000"><br></span><span style=3D"color:#008">auto</span><span sty=
le=3D"color:#000"> y </span><span style=3D"color:#660">=3D</span><span styl=
e=3D"color:#000"> </span><span style=3D"color:#660">*</span><span style=3D"=
color:#008">reinterpret_cast</span><span style=3D"color:#660">&lt;</span><s=
pan style=3D"color:#008">float</span><span style=3D"color:#000"> </span><sp=
an style=3D"color:#008">const</span><span style=3D"color:#660">*&gt;(</span=
><span style=3D"color:#000">x</span><span style=3D"color:#660">);</span><sp=
an style=3D"color:#000"><br></span></div></code></div><br></blockquote><div=
><br></div><div>That looks to me like a value cast of 42 to float, but woul=
d actually be a reinterpreting cast. In the cases where you give a prvalue =
or cast to a non-reference type, currently reinterpret_cast only does value=
 casts, and never directly reinterprets object representations with certain=
 types.&nbsp;</div><div><br></div><div>So IMO this would be very confusing.=
 You can do all that in one single line anyway (and earn UB)</div><div><br>=
</div><div>&nbsp; &nbsp; auto y =3D reinterpret_cast&lt;const float&amp;&gt=
;((const int&amp;)42);</div><blockquote class=3D"gmail_quote" style=3D"marg=
in: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><b=
r></blockquote>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_805_22212844.1361402533551--

.