Topic: `bit_cast` and when UB happens.


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Sun, 13 Jan 2019 12:12:16 -0800 (PST)
Raw View
------=_Part_1364_248131355.1547410336462
Content-Type: multipart/alternative;
 boundary="----=_Part_1365_1819358766.1547410336463"

------=_Part_1365_1819358766.1547410336463
Content-Type: text/plain; charset="UTF-8"

P0476R2 specifies when a `bit_cast` yields UB <https://wg21.link/P0476R2>:

If there is no value of type `To` corresponding to the value representation
> produced, the behavior is undefined.
>

OK, sure. But what does this really mean? Consider the following type:

class specific_bits
{
public:
    specific_bits() = default;
    explicit specific_bits(int j) : j_(j) {}

    int get_i() const {return i_;}

private:
    int j_;
    int i_ = 5;
};

Now, consider the following operation:

auto sb = bit_cast<specific_bits>(pair<int, int>{10, 20});

Let's assume that the sizes of these types are the same. `pair` is
TriviallyCopyable, as is `specific_bits`. So, is this well-defined behavior?

Well, `specific_bits` is technically just a pair of integers, so it seems
like it should be well-defined. However, it is my understanding that it is
essentially impossible to change the `i_` member of any `specific_bits`
object to any other value. At least, not from outside of the class;
anything which would do so would run into UB at some point.

As such, it would be reasonable to say that the value representation of a
`specific_bits` object includes the fact that `specific_bits::i_` shall
always be 5. Given that copying the bits does not lead to a legal value
representation of `specific_bits`, then it should be undefined behavior.

If it is UB, does that mean that the following isn't UB?

auto sb = bit_cast<specific_bits>(pair<int, int>{10, 5});

That this is well-defined behavior because it just so happens to put the
expected bits into the right place?

I'm starting to wonder if it makes sense to be able to `bit_cast` to a type
which is not trivially default constructible (and thus could theoretically
assume any value, since it can be initialized to unspecified values).

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/752cc6a4-24d3-4e86-85a0-3ab0053008d7%40isocpp.org.

------=_Part_1365_1819358766.1547410336463
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><a href=3D"https://wg21.link/P0476R2">P0476R2 specifies wh=
en a `bit_cast` yields UB</a>:<br><br><blockquote class=3D"gmail_quote" sty=
le=3D"margin: 0px 0px 0px 0.8ex; border-left: 1px solid rgb(204, 204, 204);=
 padding-left: 1ex;">If there is no value of type `To` corresponding to the=
 value representation produced, the behavior is undefined.<br></blockquote>=
<br>OK, sure. But what does this really mean? Consider the following type:<=
br><br><div style=3D"background-color: rgb(250, 250, 250); border-color: rg=
b(187, 187, 187); border-style: solid; border-width: 1px; overflow-wrap: br=
eak-word;" class=3D"prettyprint"><code class=3D"prettyprint"><div class=3D"=
subprettyprint"><span style=3D"color: #008;" class=3D"styled-by-prettify">c=
lass</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> speci=
fic_bits<br></span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></s=
pan><span style=3D"color: #008;" class=3D"styled-by-prettify">public</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">:</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 specific=
_bits</span><span style=3D"color: #660;" class=3D"styled-by-prettify">()</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span=
 style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">default</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 </span><span style=3D"color: #0=
08;" class=3D"styled-by-prettify">explicit</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"> specific_bits</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #008;" cl=
ass=3D"styled-by-prettify">int</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> j</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">)</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">:</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> j_</span><span=
 style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify">j</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">)</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">{}</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"><br>=C2=A0 =C2=A0 <br>=C2=A0 =C2=A0 </span><span style=3D"color: #=
008;" class=3D"styled-by-prettify">int</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify"> get_i</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">()</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-pre=
ttify">const</span><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">return</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> i_</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">;}</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"><br><br></span><span style=3D"color=
: #008;" class=3D"styled-by-prettify">private</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">:</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 </span><span style=3D"color: #0=
08;" class=3D"styled-by-prettify">int</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"> j_</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify"><br>=C2=A0 =C2=A0 </span><span style=3D"color: #008;" class=3D"s=
tyled-by-prettify">int</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"> i_ </span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
</span><span style=3D"color: #066;" class=3D"styled-by-prettify">5</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">};</span></div></code></div><br>No=
w, consider the following operation:<br><br><div style=3D"background-color:=
 rgb(250, 250, 250); border-color: rgb(187, 187, 187); border-style: solid;=
 border-width: 1px; overflow-wrap: break-word;" class=3D"prettyprint"><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"> sb </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> bit_cast</span><span style=3D"color: #080;" class=3D"styl=
ed-by-prettify">&lt;specific_bits&gt;</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify">pair</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">&lt;</span><span style=3D"color: #008;" class=3D"styled-by-pret=
tify">int</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #008;" class=3D"styled-by-prettify">int</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">&gt;{</span><span style=3D=
"color: #066;" class=3D"styled-by-prettify">10</span><span style=3D"color: =
#660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;" cl=
ass=3D"styled-by-prettify"> </span><span style=3D"color: #066;" class=3D"st=
yled-by-prettify">20</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">});</span></div></code></div><br>Let&#39;s assume that the sizes =
of these types are the same. `pair` is TriviallyCopyable, as is `specific_b=
its`. So, is this well-defined behavior?<br><br>Well, `specific_bits` is te=
chnically just a pair of integers, so it seems like it should be well-defin=
ed. However, it is my understanding that it is essentially impossible to ch=
ange the `i_` member of any `specific_bits` object to any other value. At l=
east, not from outside of the class; anything which would do so would run i=
nto UB at some point.<br><br>As such, it would be reasonable to say that th=
e value representation of a `specific_bits` object includes the fact that `=
specific_bits::i_` shall always be 5. Given that copying the bits does not =
lead to a legal value representation of `specific_bits`, then it should be =
undefined behavior.<br><br>If it is UB, does that mean that the following i=
sn&#39;t UB?<br><br><div style=3D"background-color: rgb(250, 250, 250); bor=
der-color: rgb(187, 187, 187); border-style: solid; border-width: 1px; over=
flow-wrap: break-word;" class=3D"prettyprint"><code class=3D"prettyprint"><=
div class=3D"subprettyprint"><span style=3D"color: #008;" class=3D"styled-b=
y-prettify">auto</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> sb </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> bit_ca=
st</span><span style=3D"color: #080;" class=3D"styled-by-prettify">&lt;spec=
ific_bits&gt;</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">pair</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</span><=
span style=3D"color: #008;" class=3D"styled-by-prettify">int</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008=
;" class=3D"styled-by-prettify">int</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">&gt;{</span><span style=3D"color: #066;" class=3D"=
styled-by-prettify">10</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> </span><span style=3D"color: #066;" class=3D"styled-by-prettify">5</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">});</span></div=
></code></div><br>That this is well-defined behavior because it just so hap=
pens to put the expected bits into the right place?<br><br>I&#39;m starting=
 to wonder if it makes sense to be able to `bit_cast` to a type which is no=
t trivially default constructible (and thus could theoretically assume any =
value, since it can be initialized to unspecified values).<br></div>

<p></p>

-- <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 <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/752cc6a4-24d3-4e86-85a0-3ab0053008d7%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/752cc6a4-24d3-4e86-85a0-3ab0053008d7=
%40isocpp.org</a>.<br />

------=_Part_1365_1819358766.1547410336463--

------=_Part_1364_248131355.1547410336462--

.


Author: "T. C." <rs2740@gmail.com>
Date: Sun, 13 Jan 2019 15:31:49 -0800 (PST)
Raw View
------=_Part_1435_1466140923.1547422309304
Content-Type: multipart/alternative;
 boundary="----=_Part_1436_478045035.1547422309304"

------=_Part_1436_478045035.1547422309304
Content-Type: text/plain; charset="UTF-8"



On Sunday, January 13, 2019 at 3:12:16 PM UTC-5, Nicol Bolas wrote:
>
> P0476R2 specifies when a `bit_cast` yields UB <https://wg21.link/P0476R2>:
>
> If there is no value of type `To` corresponding to the value
>> representation produced, the behavior is undefined.
>>
>
> OK, sure. But what does this really mean? Consider the following type:
>
> class specific_bits
> {
> public:
>     specific_bits() = default;
>     explicit specific_bits(int j) : j_(j) {}
>
>     int get_i() const {return i_;}
>
> private:
>     int j_;
>     int i_ = 5;
> };
>
> Now, consider the following operation:
>
> auto sb = bit_cast<specific_bits>(pair<int, int>{10, 20});
>
> Let's assume that the sizes of these types are the same. `pair` is
> TriviallyCopyable, as is `specific_bits`. So, is this well-defined behavior?
>
> Well, `specific_bits` is technically just a pair of integers, so it seems
> like it should be well-defined. However, it is my understanding that it is
> essentially impossible to change the `i_` member of any `specific_bits`
> object to any other value. At least, not from outside of the class;
> anything which would do so would run into UB at some point.
>

Access control is not watertight enough for that. There are ways you can
get your hands on a pointer to member to specific_bits::i_, at which point
you can modify the field
(http://coliru.stacked-crooked.com/a/62dd7c4bfeb1eb96).

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/0119543e-6d78-458a-af85-35f122a0b396%40isocpp.org.

------=_Part_1436_478045035.1547422309304
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>On Sunday, January 13, 2019 at 3:12:16 PM UTC-5, N=
icol Bolas wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margi=
n-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"l=
tr"><a href=3D"https://wg21.link/P0476R2" target=3D"_blank" rel=3D"nofollow=
" onmousedown=3D"this.href=3D&#39;https://www.google.com/url?q\x3dhttps%3A%=
2F%2Fwg21.link%2FP0476R2\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHU1KWervqE=
c0InXWzO9mTSAqvpKg&#39;;return true;" onclick=3D"this.href=3D&#39;https://w=
ww.google.com/url?q\x3dhttps%3A%2F%2Fwg21.link%2FP0476R2\x26sa\x3dD\x26sntz=
\x3d1\x26usg\x3dAFQjCNHU1KWervqEc0InXWzO9mTSAqvpKg&#39;;return true;">P0476=
R2 specifies when a `bit_cast` yields UB</a>:<br><br><blockquote class=3D"g=
mail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204=
,204,204);padding-left:1ex">If there is no value of type `To` corresponding=
 to the value representation produced, the behavior is undefined.<br></bloc=
kquote><br>OK, sure. But what does this really mean? Consider the following=
 type:<br><br><div style=3D"background-color:rgb(250,250,250);border-color:=
rgb(187,187,187);border-style:solid;border-width:1px"><code><div><span styl=
e=3D"color:#008">class</span><span style=3D"color:#000"> specific_bits<br><=
/span><span style=3D"color:#660">{</span><span style=3D"color:#000"><br></s=
pan><span style=3D"color:#008">public</span><span style=3D"color:#660">:</s=
pan><span style=3D"color:#000"><br>=C2=A0 =C2=A0 specific_bits</span><span =
style=3D"color:#660">()</span><span style=3D"color:#000"> </span><span styl=
e=3D"color:#660">=3D</span><span style=3D"color:#000"> </span><span style=
=3D"color:#008">default</span><span style=3D"color:#660">;</span><span styl=
e=3D"color:#000"><br>=C2=A0 =C2=A0 </span><span style=3D"color:#008">explic=
it</span><span style=3D"color:#000"> specific_bits</span><span style=3D"col=
or:#660">(</span><span style=3D"color:#008">int</span><span style=3D"color:=
#000"> j</span><span style=3D"color:#660">)</span><span style=3D"color:#000=
"> </span><span style=3D"color:#660">:</span><span style=3D"color:#000"> j_=
</span><span style=3D"color:#660">(</span><span style=3D"color:#000">j</spa=
n><span style=3D"color:#660">)</span><span style=3D"color:#000"> </span><sp=
an style=3D"color:#660">{}</span><span style=3D"color:#000"><br>=C2=A0 =C2=
=A0 <br>=C2=A0 =C2=A0 </span><span style=3D"color:#008">int</span><span sty=
le=3D"color:#000"> get_i</span><span style=3D"color:#660">()</span><span st=
yle=3D"color:#000"> </span><span style=3D"color:#008">const</span><span sty=
le=3D"color:#000"> </span><span style=3D"color:#660">{</span><span style=3D=
"color:#008">return</span><span style=3D"color:#000"> i_</span><span style=
=3D"color:#660">;}</span><span style=3D"color:#000"><br><br></span><span st=
yle=3D"color:#008">private</span><span style=3D"color:#660">:</span><span s=
tyle=3D"color:#000"><br>=C2=A0 =C2=A0 </span><span style=3D"color:#008">int=
</span><span style=3D"color:#000"> j_</span><span style=3D"color:#660">;</s=
pan><span style=3D"color:#000"><br>=C2=A0 =C2=A0 </span><span style=3D"colo=
r:#008">int</span><span style=3D"color:#000"> i_ </span><span style=3D"colo=
r:#660">=3D</span><span style=3D"color:#000"> </span><span style=3D"color:#=
066">5</span><span style=3D"color:#660">;</span><span style=3D"color:#000">=
<br></span><span style=3D"color:#660">};</span></div></code></div><br>Now, =
consider the following operation:<br><br><div style=3D"background-color:rgb=
(250,250,250);border-color:rgb(187,187,187);border-style:solid;border-width=
:1px"><code><div><span style=3D"color:#008">auto</span><span style=3D"color=
:#000"> sb </span><span style=3D"color:#660">=3D</span><span style=3D"color=
:#000"> bit_cast</span><span style=3D"color:#080">&lt;specific_bits&gt;</sp=
an><span style=3D"color:#660">(</span><span style=3D"color:#000">pair</span=
><span style=3D"color:#660">&lt;</span><span style=3D"color:#008">i<wbr>nt<=
/span><span style=3D"color:#660">,</span><span style=3D"color:#000"> </span=
><span style=3D"color:#008">int</span><span style=3D"color:#660">&gt;{</spa=
n><span style=3D"color:#066">10</span><span style=3D"color:#660">,</span><s=
pan style=3D"color:#000"> </span><span style=3D"color:#066">20</span><span =
style=3D"color:#660">});</span></div></code></div><br>Let&#39;s assume that=
 the sizes of these types are the same. `pair` is TriviallyCopyable, as is =
`specific_bits`. So, is this well-defined behavior?<br><br>Well, `specific_=
bits` is technically just a pair of integers, so it seems like it should be=
 well-defined. However, it is my understanding that it is essentially impos=
sible to change the `i_` member of any `specific_bits` object to any other =
value. At least, not from outside of the class; anything which would do so =
would run into UB at some point.<br></div></blockquote><div><br></div><div>=
Access control is not watertight enough for that. There are ways you can ge=
t your hands on a pointer to member to specific_bits::i_, at which point yo=
u can modify the field (http://coliru.stacked-crooked.com/a/62dd7c4bfeb1eb9=
6).</div></div>

<p></p>

-- <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 <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/0119543e-6d78-458a-af85-35f122a0b396%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/0119543e-6d78-458a-af85-35f122a0b396=
%40isocpp.org</a>.<br />

------=_Part_1436_478045035.1547422309304--

------=_Part_1435_1466140923.1547422309304--

.


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Sun, 13 Jan 2019 16:27:23 -0800 (PST)
Raw View
------=_Part_1403_1208934875.1547425643693
Content-Type: multipart/alternative;
 boundary="----=_Part_1404_285477226.1547425643694"

------=_Part_1404_285477226.1547425643694
Content-Type: text/plain; charset="UTF-8"



On Sunday, January 13, 2019 at 6:31:49 PM UTC-5, T. C. wrote:
>
>
>
> On Sunday, January 13, 2019 at 3:12:16 PM UTC-5, Nicol Bolas wrote:
>>
>> P0476R2 specifies when a `bit_cast` yields UB <https://wg21.link/P0476R2>
>> :
>>
>> If there is no value of type `To` corresponding to the value
>>> representation produced, the behavior is undefined.
>>>
>>
>> OK, sure. But what does this really mean? Consider the following type:
>>
>> class specific_bits
>> {
>> public:
>>     specific_bits() = default;
>>     explicit specific_bits(int j) : j_(j) {}
>>
>>     int get_i() const {return i_;}
>>
>> private:
>>     int j_;
>>     int i_ = 5;
>> };
>>
>> Now, consider the following operation:
>>
>> auto sb = bit_cast<specific_bits>(pair<int, int>{10, 20});
>>
>> Let's assume that the sizes of these types are the same. `pair` is
>> TriviallyCopyable, as is `specific_bits`. So, is this well-defined behavior?
>>
>> Well, `specific_bits` is technically just a pair of integers, so it seems
>> like it should be well-defined. However, it is my understanding that it is
>> essentially impossible to change the `i_` member of any `specific_bits`
>> object to any other value. At least, not from outside of the class;
>> anything which would do so would run into UB at some point.
>>
>
> Access control is not watertight enough for that. There are ways you can
> get your hands on a pointer to member to specific_bits::i_, at which point
> you can modify the field (
> http://coliru.stacked-crooked.com/a/62dd7c4bfeb1eb96).
>

Why isn't `template struct accessor<specific_bits, &specific_bits::i_>;` a
compile error? Doesn't it mention a name which is not accessible from that
location?

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/0c937122-b9e9-4258-a320-19fede2bfe22%40isocpp.org.

------=_Part_1404_285477226.1547425643694
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>On Sunday, January 13, 2019 at 6:31:49 PM UTC-5, T=
.. C. wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left=
: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"><b=
r><br>On Sunday, January 13, 2019 at 3:12:16 PM UTC-5, Nicol Bolas wrote:<b=
lockquote class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-=
left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><a href=3D"https://w=
g21.link/P0476R2" rel=3D"nofollow" target=3D"_blank" onmousedown=3D"this.hr=
ef=3D&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fwg21.link%2FP0476R2=
\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHU1KWervqEc0InXWzO9mTSAqvpKg&#39;;=
return true;" onclick=3D"this.href=3D&#39;https://www.google.com/url?q\x3dh=
ttps%3A%2F%2Fwg21.link%2FP0476R2\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHU=
1KWervqEc0InXWzO9mTSAqvpKg&#39;;return true;">P0476R2 specifies when a `bit=
_cast` yields UB</a>:<br><br><blockquote class=3D"gmail_quote" style=3D"mar=
gin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1=
ex">If there is no value of type `To` corresponding to the value representa=
tion produced, the behavior is undefined.<br></blockquote><br>OK, sure. But=
 what does this really mean? Consider the following type:<br><br><div style=
=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,187);border-=
style:solid;border-width:1px"><code><div><span style=3D"color:#008">class</=
span><span style=3D"color:#000"> specific_bits<br></span><span style=3D"col=
or:#660">{</span><span style=3D"color:#000"><br></span><span style=3D"color=
:#008">public</span><span style=3D"color:#660">:</span><span style=3D"color=
:#000"><br>=C2=A0 =C2=A0 specific_bits</span><span style=3D"color:#660">()<=
/span><span style=3D"color:#000"> </span><span style=3D"color:#660">=3D</sp=
an><span style=3D"color:#000"> </span><span style=3D"color:#008">default</s=
pan><span style=3D"color:#660">;</span><span style=3D"color:#000"><br>=C2=
=A0 =C2=A0 </span><span style=3D"color:#008">explicit</span><span style=3D"=
color:#000"> specific_bits</span><span style=3D"color:#660">(</span><span s=
tyle=3D"color:#008">int</span><span style=3D"color:#000"> j</span><span sty=
le=3D"color:#660">)</span><span style=3D"color:#000"> </span><span style=3D=
"color:#660">:</span><span style=3D"color:#000"> j_</span><span style=3D"co=
lor:#660">(</span><span style=3D"color:#000">j</span><span style=3D"color:#=
660">)</span><span style=3D"color:#000"> </span><span style=3D"color:#660">=
{}</span><span style=3D"color:#000"><br>=C2=A0 =C2=A0 <br>=C2=A0 =C2=A0 </s=
pan><span style=3D"color:#008">int</span><span style=3D"color:#000"> get_i<=
/span><span style=3D"color:#660">()</span><span style=3D"color:#000"> </spa=
n><span style=3D"color:#008">const</span><span style=3D"color:#000"> </span=
><span style=3D"color:#660">{</span><span style=3D"color:#008">return</span=
><span style=3D"color:#000"> i_</span><span style=3D"color:#660">;}</span><=
span style=3D"color:#000"><br><br></span><span style=3D"color:#008">private=
</span><span style=3D"color:#660">:</span><span style=3D"color:#000"><br>=
=C2=A0 =C2=A0 </span><span style=3D"color:#008">int</span><span style=3D"co=
lor:#000"> j_</span><span style=3D"color:#660">;</span><span style=3D"color=
:#000"><br>=C2=A0 =C2=A0 </span><span style=3D"color:#008">int</span><span =
style=3D"color:#000"> i_ </span><span style=3D"color:#660">=3D</span><span =
style=3D"color:#000"> </span><span style=3D"color:#066">5</span><span style=
=3D"color:#660">;</span><span style=3D"color:#000"><br></span><span style=
=3D"color:#660">};</span></div></code></div><br>Now, consider the following=
 operation:<br><br><div style=3D"background-color:rgb(250,250,250);border-c=
olor:rgb(187,187,187);border-style:solid;border-width:1px"><code><div><span=
 style=3D"color:#008">auto</span><span style=3D"color:#000"> sb </span><spa=
n style=3D"color:#660">=3D</span><span style=3D"color:#000"> bit_cast</span=
><span style=3D"color:#080">&lt;specific_bits&gt;</span><span style=3D"colo=
r:#660">(</span><span style=3D"color:#000">pair</span><span style=3D"color:=
#660">&lt;</span><span style=3D"color:#008">i<wbr>nt</span><span style=3D"c=
olor:#660">,</span><span style=3D"color:#000"> </span><span style=3D"color:=
#008">int</span><span style=3D"color:#660">&gt;{</span><span style=3D"color=
:#066">10</span><span style=3D"color:#660">,</span><span style=3D"color:#00=
0"> </span><span style=3D"color:#066">20</span><span style=3D"color:#660">}=
);</span></div></code></div><br>Let&#39;s assume that the sizes of these ty=
pes are the same. `pair` is TriviallyCopyable, as is `specific_bits`. So, i=
s this well-defined behavior?<br><br>Well, `specific_bits` is technically j=
ust a pair of integers, so it seems like it should be well-defined. However=
, it is my understanding that it is essentially impossible to change the `i=
_` member of any `specific_bits` object to any other value. At least, not f=
rom outside of the class; anything which would do so would run into UB at s=
ome point.<br></div></blockquote><div><br></div><div>Access control is not =
watertight enough for that. There are ways you can get your hands on a poin=
ter to member to specific_bits::i_, at which point you can modify the field=
 (<a href=3D"http://coliru.stacked-crooked.com/a/62dd7c4bfeb1eb96" target=
=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;http://www.goo=
gle.com/url?q\x3dhttp%3A%2F%2Fcoliru.stacked-crooked.com%2Fa%2F62dd7c4bfeb1=
eb96\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNH71AB-x8a9jTeFa2WdcomHfbPRiw&#=
39;;return true;" onclick=3D"this.href=3D&#39;http://www.google.com/url?q\x=
3dhttp%3A%2F%2Fcoliru.stacked-crooked.com%2Fa%2F62dd7c4bfeb1eb96\x26sa\x3dD=
\x26sntz\x3d1\x26usg\x3dAFQjCNH71AB-x8a9jTeFa2WdcomHfbPRiw&#39;;return true=
;">http://coliru.stacked-<wbr>crooked.com/a/62dd7c4bfeb1eb96</a><wbr>).</di=
v></div></blockquote><div><br></div><div>Why isn&#39;t `template struct acc=
essor&lt;specific_bits, &amp;specific_bits::i_&gt;;` a compile error? Doesn=
&#39;t it mention a name which is not accessible from that location?<br></d=
iv></div>

<p></p>

-- <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 <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/0c937122-b9e9-4258-a320-19fede2bfe22%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/0c937122-b9e9-4258-a320-19fede2bfe22=
%40isocpp.org</a>.<br />

------=_Part_1404_285477226.1547425643694--

------=_Part_1403_1208934875.1547425643693--

.


Author: "T. C." <rs2740@gmail.com>
Date: Sun, 13 Jan 2019 17:53:02 -0800 (PST)
Raw View
------=_Part_1413_1306290076.1547430782531
Content-Type: multipart/alternative;
 boundary="----=_Part_1414_1638471212.1547430782531"

------=_Part_1414_1638471212.1547430782531
Content-Type: text/plain; charset="UTF-8"



On Sunday, January 13, 2019 at 7:27:23 PM UTC-5, Nicol Bolas wrote:
>
>
>
> On Sunday, January 13, 2019 at 6:31:49 PM UTC-5, T. C. wrote:
>>
>>
>>
>> On Sunday, January 13, 2019 at 3:12:16 PM UTC-5, Nicol Bolas wrote:
>>>
>>> P0476R2 specifies when a `bit_cast` yields UB
>>> <https://wg21.link/P0476R2>:
>>>
>>> If there is no value of type `To` corresponding to the value
>>>> representation produced, the behavior is undefined.
>>>>
>>>
>>> OK, sure. But what does this really mean? Consider the following type:
>>>
>>> class specific_bits
>>> {
>>> public:
>>>     specific_bits() = default;
>>>     explicit specific_bits(int j) : j_(j) {}
>>>
>>>     int get_i() const {return i_;}
>>>
>>> private:
>>>     int j_;
>>>     int i_ = 5;
>>> };
>>>
>>> Now, consider the following operation:
>>>
>>> auto sb = bit_cast<specific_bits>(pair<int, int>{10, 20});
>>>
>>> Let's assume that the sizes of these types are the same. `pair` is
>>> TriviallyCopyable, as is `specific_bits`. So, is this well-defined behavior?
>>>
>>> Well, `specific_bits` is technically just a pair of integers, so it
>>> seems like it should be well-defined. However, it is my understanding that
>>> it is essentially impossible to change the `i_` member of any
>>> `specific_bits` object to any other value. At least, not from outside of
>>> the class; anything which would do so would run into UB at some point.
>>>
>>
>> Access control is not watertight enough for that. There are ways you can
>> get your hands on a pointer to member to specific_bits::i_, at which point
>> you can modify the field (
>> http://coliru.stacked-crooked.com/a/62dd7c4bfeb1eb96).
>>
>
> Why isn't `template struct accessor<specific_bits, &specific_bits::i_>;` a
> compile error? Doesn't it mention a name which is not accessible from that
> location?
>

Access checking is not applied to explicit instantiations (with exceptions
not relevant here). See [temp.spec]/6 in the current WP.

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e9d6281d-0256-437a-832f-62d2b885c552%40isocpp.org.

------=_Part_1414_1638471212.1547430782531
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>On Sunday, January 13, 2019 at 7:27:23 PM UTC-5, N=
icol Bolas wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margi=
n-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"l=
tr"><br><br>On Sunday, January 13, 2019 at 6:31:49 PM UTC-5, T. C. wrote:<b=
lockquote class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-=
left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><br><br>On Sunday, J=
anuary 13, 2019 at 3:12:16 PM UTC-5, Nicol Bolas wrote:<blockquote class=3D=
"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc soli=
d;padding-left:1ex"><div dir=3D"ltr"><a href=3D"https://wg21.link/P0476R2" =
rel=3D"nofollow" target=3D"_blank" onmousedown=3D"this.href=3D&#39;https://=
www.google.com/url?q\x3dhttps%3A%2F%2Fwg21.link%2FP0476R2\x26sa\x3dD\x26snt=
z\x3d1\x26usg\x3dAFQjCNHU1KWervqEc0InXWzO9mTSAqvpKg&#39;;return true;" oncl=
ick=3D"this.href=3D&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fwg21.=
link%2FP0476R2\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHU1KWervqEc0InXWzO9m=
TSAqvpKg&#39;;return true;">P0476R2 specifies when a `bit_cast` yields UB</=
a>:<br><br><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.=
8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">If there is no=
 value of type `To` corresponding to the value representation produced, the=
 behavior is undefined.<br></blockquote><br>OK, sure. But what does this re=
ally mean? Consider the following type:<br><br><div style=3D"background-col=
or:rgb(250,250,250);border-color:rgb(187,187,187);border-style:solid;border=
-width:1px"><code><div><span style=3D"color:#008">class</span><span style=
=3D"color:#000"> specific_bits<br></span><span style=3D"color:#660">{</span=
><span style=3D"color:#000"><br></span><span style=3D"color:#008">public</s=
pan><span style=3D"color:#660">:</span><span style=3D"color:#000"><br>=C2=
=A0 =C2=A0 specific_bits</span><span style=3D"color:#660">()</span><span st=
yle=3D"color:#000"> </span><span style=3D"color:#660">=3D</span><span style=
=3D"color:#000"> </span><span style=3D"color:#008">default</span><span styl=
e=3D"color:#660">;</span><span style=3D"color:#000"><br>=C2=A0 =C2=A0 </spa=
n><span style=3D"color:#008">explicit</span><span style=3D"color:#000"> spe=
cific_bits</span><span style=3D"color:#660">(</span><span style=3D"color:#0=
08">int</span><span style=3D"color:#000"> j</span><span style=3D"color:#660=
">)</span><span style=3D"color:#000"> </span><span style=3D"color:#660">:</=
span><span style=3D"color:#000"> j_</span><span style=3D"color:#660">(</spa=
n><span style=3D"color:#000">j</span><span style=3D"color:#660">)</span><sp=
an style=3D"color:#000"> </span><span style=3D"color:#660">{}</span><span s=
tyle=3D"color:#000"><br>=C2=A0 =C2=A0 <br>=C2=A0 =C2=A0 </span><span style=
=3D"color:#008">int</span><span style=3D"color:#000"> get_i</span><span sty=
le=3D"color:#660">()</span><span style=3D"color:#000"> </span><span style=
=3D"color:#008">const</span><span style=3D"color:#000"> </span><span style=
=3D"color:#660">{</span><span style=3D"color:#008">return</span><span style=
=3D"color:#000"> i_</span><span style=3D"color:#660">;}</span><span style=
=3D"color:#000"><br><br></span><span style=3D"color:#008">private</span><sp=
an style=3D"color:#660">:</span><span style=3D"color:#000"><br>=C2=A0 =C2=
=A0 </span><span style=3D"color:#008">int</span><span style=3D"color:#000">=
 j_</span><span style=3D"color:#660">;</span><span style=3D"color:#000"><br=
>=C2=A0 =C2=A0 </span><span style=3D"color:#008">int</span><span style=3D"c=
olor:#000"> i_ </span><span style=3D"color:#660">=3D</span><span style=3D"c=
olor:#000"> </span><span style=3D"color:#066">5</span><span style=3D"color:=
#660">;</span><span style=3D"color:#000"><br></span><span style=3D"color:#6=
60">};</span></div></code></div><br>Now, consider the following operation:<=
br><br><div style=3D"background-color:rgb(250,250,250);border-color:rgb(187=
,187,187);border-style:solid;border-width:1px"><code><div><span style=3D"co=
lor:#008">auto</span><span style=3D"color:#000"> sb </span><span style=3D"c=
olor:#660">=3D</span><span style=3D"color:#000"> bit_cast</span><span style=
=3D"color:#080">&lt;specific_bits&gt;</span><span style=3D"color:#660">(</s=
pan><span style=3D"color:#000">pair</span><span style=3D"color:#660">&lt;</=
span><span style=3D"color:#008">i<wbr>nt</span><span style=3D"color:#660">,=
</span><span style=3D"color:#000"> </span><span style=3D"color:#008">int</s=
pan><span style=3D"color:#660">&gt;{</span><span style=3D"color:#066">10</s=
pan><span style=3D"color:#660">,</span><span style=3D"color:#000"> </span><=
span style=3D"color:#066">20</span><span style=3D"color:#660">});</span></d=
iv></code></div><br>Let&#39;s assume that the sizes of these types are the =
same. `pair` is TriviallyCopyable, as is `specific_bits`. So, is this well-=
defined behavior?<br><br>Well, `specific_bits` is technically just a pair o=
f integers, so it seems like it should be well-defined. However, it is my u=
nderstanding that it is essentially impossible to change the `i_` member of=
 any `specific_bits` object to any other value. At least, not from outside =
of the class; anything which would do so would run into UB at some point.<b=
r></div></blockquote><div><br></div><div>Access control is not watertight e=
nough for that. There are ways you can get your hands on a pointer to membe=
r to specific_bits::i_, at which point you can modify the field (<a href=3D=
"http://coliru.stacked-crooked.com/a/62dd7c4bfeb1eb96" rel=3D"nofollow" tar=
get=3D"_blank" onmousedown=3D"this.href=3D&#39;http://www.google.com/url?q\=
x3dhttp%3A%2F%2Fcoliru.stacked-crooked.com%2Fa%2F62dd7c4bfeb1eb96\x26sa\x3d=
D\x26sntz\x3d1\x26usg\x3dAFQjCNH71AB-x8a9jTeFa2WdcomHfbPRiw&#39;;return tru=
e;" onclick=3D"this.href=3D&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2=
Fcoliru.stacked-crooked.com%2Fa%2F62dd7c4bfeb1eb96\x26sa\x3dD\x26sntz\x3d1\=
x26usg\x3dAFQjCNH71AB-x8a9jTeFa2WdcomHfbPRiw&#39;;return true;">http://coli=
ru.stacked-<wbr>crooked.com/a/62dd7c4bfeb1eb96</a><wbr>).</div></div></bloc=
kquote><div><br></div><div>Why isn&#39;t `template struct accessor&lt;speci=
fic_bits, &amp;specific_bits::i_&gt;;` a compile error? Doesn&#39;t it ment=
ion a name which is not accessible from that location?<br></div></div></blo=
ckquote><div><br></div><div>Access checking is not applied to explicit inst=
antiations (with exceptions not relevant here). See [temp.spec]/6 in the cu=
rrent WP.=C2=A0</div></div>

<p></p>

-- <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 <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/e9d6281d-0256-437a-832f-62d2b885c552%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/e9d6281d-0256-437a-832f-62d2b885c552=
%40isocpp.org</a>.<br />

------=_Part_1414_1638471212.1547430782531--

------=_Part_1413_1306290076.1547430782531--

.


Author: Andrew Tomazos <andrewtomazos@gmail.com>
Date: Sun, 13 Jan 2019 20:18:02 -0600
Raw View
--000000000000474ad5057f61a8b8
Content-Type: text/plain; charset="UTF-8"

https://stackoverflow.com/q/54175056/1131467

On Sun, Jan 13, 2019 at 7:53 PM T. C. <rs2740@gmail.com> wrote:

>
>
> On Sunday, January 13, 2019 at 7:27:23 PM UTC-5, Nicol Bolas wrote:
>>
>>
>>
>> On Sunday, January 13, 2019 at 6:31:49 PM UTC-5, T. C. wrote:
>>>
>>>
>>>
>>> On Sunday, January 13, 2019 at 3:12:16 PM UTC-5, Nicol Bolas wrote:
>>>>
>>>> P0476R2 specifies when a `bit_cast` yields UB
>>>> <https://wg21.link/P0476R2>:
>>>>
>>>> If there is no value of type `To` corresponding to the value
>>>>> representation produced, the behavior is undefined.
>>>>>
>>>>
>>>> OK, sure. But what does this really mean? Consider the following type:
>>>>
>>>> class specific_bits
>>>> {
>>>> public:
>>>>     specific_bits() = default;
>>>>     explicit specific_bits(int j) : j_(j) {}
>>>>
>>>>     int get_i() const {return i_;}
>>>>
>>>> private:
>>>>     int j_;
>>>>     int i_ = 5;
>>>> };
>>>>
>>>> Now, consider the following operation:
>>>>
>>>> auto sb = bit_cast<specific_bits>(pair<int, int>{10, 20});
>>>>
>>>> Let's assume that the sizes of these types are the same. `pair` is
>>>> TriviallyCopyable, as is `specific_bits`. So, is this well-defined behavior?
>>>>
>>>> Well, `specific_bits` is technically just a pair of integers, so it
>>>> seems like it should be well-defined. However, it is my understanding that
>>>> it is essentially impossible to change the `i_` member of any
>>>> `specific_bits` object to any other value. At least, not from outside of
>>>> the class; anything which would do so would run into UB at some point.
>>>>
>>>
>>> Access control is not watertight enough for that. There are ways you can
>>> get your hands on a pointer to member to specific_bits::i_, at which point
>>> you can modify the field (
>>> http://coliru.stacked-crooked.com/a/62dd7c4bfeb1eb96).
>>>
>>
>> Why isn't `template struct accessor<specific_bits, &specific_bits::i_>;`
>> a compile error? Doesn't it mention a name which is not accessible from
>> that location?
>>
>
> Access checking is not applied to explicit instantiations (with exceptions
> not relevant here). See [temp.spec]/6 in the current WP.
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e9d6281d-0256-437a-832f-62d2b885c552%40isocpp.org
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e9d6281d-0256-437a-832f-62d2b885c552%40isocpp.org?utm_medium=email&utm_source=footer>
> .
>

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAB%2B4KHL-Kgk3rM2pujOs4yhrRw0EU8_7Hx%3DkiMqF-oRthj14MA%40mail.gmail.com.

--000000000000474ad5057f61a8b8
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><a href=3D"https://stackoverflow.com/q/54=
175056/1131467">https://stackoverflow.com/q/54175056/1131467</a><br></div><=
/div><br><div class=3D"gmail_quote"><div dir=3D"ltr">On Sun, Jan 13, 2019 a=
t 7:53 PM T. C. &lt;<a href=3D"mailto:rs2740@gmail.com">rs2740@gmail.com</a=
>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px=
 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><di=
v dir=3D"ltr"><br><br>On Sunday, January 13, 2019 at 7:27:23 PM UTC-5, Nico=
l Bolas wrote:<blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px=
 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D=
"ltr"><br><br>On Sunday, January 13, 2019 at 6:31:49 PM UTC-5, T. C. wrote:=
<blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-=
left:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr"><br><br>=
On Sunday, January 13, 2019 at 3:12:16 PM UTC-5, Nicol Bolas wrote:<blockqu=
ote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px=
 solid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr"><a href=3D"https=
://wg21.link/P0476R2" rel=3D"nofollow" target=3D"_blank">P0476R2 specifies =
when a `bit_cast` yields UB</a>:<br><br><blockquote class=3D"gmail_quote" s=
tyle=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);pad=
ding-left:1ex">If there is no value of type `To` corresponding to the value=
 representation produced, the behavior is undefined.<br></blockquote><br>OK=
, sure. But what does this really mean? Consider the following type:<br><br=
><div style=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,1=
87);border-style:solid;border-width:1px"><code><div><span style=3D"color:rg=
b(0,0,136)">class</span><span style=3D"color:rgb(0,0,0)"> specific_bits<br>=
</span><span style=3D"color:rgb(102,102,0)">{</span><span style=3D"color:rg=
b(0,0,0)"><br></span><span style=3D"color:rgb(0,0,136)">public</span><span =
style=3D"color:rgb(102,102,0)">:</span><span style=3D"color:rgb(0,0,0)"><br=
>=C2=A0 =C2=A0 specific_bits</span><span style=3D"color:rgb(102,102,0)">()<=
/span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102=
,102,0)">=3D</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"=
color:rgb(0,0,136)">default</span><span style=3D"color:rgb(102,102,0)">;</s=
pan><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 </span><span style=
=3D"color:rgb(0,0,136)">explicit</span><span style=3D"color:rgb(0,0,0)"> sp=
ecific_bits</span><span style=3D"color:rgb(102,102,0)">(</span><span style=
=3D"color:rgb(0,0,136)">int</span><span style=3D"color:rgb(0,0,0)"> j</span=
><span style=3D"color:rgb(102,102,0)">)</span><span style=3D"color:rgb(0,0,=
0)"> </span><span style=3D"color:rgb(102,102,0)">:</span><span style=3D"col=
or:rgb(0,0,0)"> j_</span><span style=3D"color:rgb(102,102,0)">(</span><span=
 style=3D"color:rgb(0,0,0)">j</span><span style=3D"color:rgb(102,102,0)">)<=
/span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(102=
,102,0)">{}</span><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 <br>=
=C2=A0 =C2=A0 </span><span style=3D"color:rgb(0,0,136)">int</span><span sty=
le=3D"color:rgb(0,0,0)"> get_i</span><span style=3D"color:rgb(102,102,0)">(=
)</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"color:rgb(0=
,0,136)">const</span><span style=3D"color:rgb(0,0,0)"> </span><span style=
=3D"color:rgb(102,102,0)">{</span><span style=3D"color:rgb(0,0,136)">return=
</span><span style=3D"color:rgb(0,0,0)"> i_</span><span style=3D"color:rgb(=
102,102,0)">;}</span><span style=3D"color:rgb(0,0,0)"><br><br></span><span =
style=3D"color:rgb(0,0,136)">private</span><span style=3D"color:rgb(102,102=
,0)">:</span><span style=3D"color:rgb(0,0,0)"><br>=C2=A0 =C2=A0 </span><spa=
n style=3D"color:rgb(0,0,136)">int</span><span style=3D"color:rgb(0,0,0)"> =
j_</span><span style=3D"color:rgb(102,102,0)">;</span><span style=3D"color:=
rgb(0,0,0)"><br>=C2=A0 =C2=A0 </span><span style=3D"color:rgb(0,0,136)">int=
</span><span style=3D"color:rgb(0,0,0)"> i_ </span><span style=3D"color:rgb=
(102,102,0)">=3D</span><span style=3D"color:rgb(0,0,0)"> </span><span style=
=3D"color:rgb(0,102,102)">5</span><span style=3D"color:rgb(102,102,0)">;</s=
pan><span style=3D"color:rgb(0,0,0)"><br></span><span style=3D"color:rgb(10=
2,102,0)">};</span></div></code></div><br>Now, consider the following opera=
tion:<br><br><div style=3D"background-color:rgb(250,250,250);border-color:r=
gb(187,187,187);border-style:solid;border-width:1px"><code><div><span style=
=3D"color:rgb(0,0,136)">auto</span><span style=3D"color:rgb(0,0,0)"> sb </s=
pan><span style=3D"color:rgb(102,102,0)">=3D</span><span style=3D"color:rgb=
(0,0,0)"> bit_cast</span><span style=3D"color:rgb(0,136,0)">&lt;specific_bi=
ts&gt;</span><span style=3D"color:rgb(102,102,0)">(</span><span style=3D"co=
lor:rgb(0,0,0)">pair</span><span style=3D"color:rgb(102,102,0)">&lt;</span>=
<span style=3D"color:rgb(0,0,136)">int</span><span style=3D"color:rgb(102,1=
02,0)">,</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"colo=
r:rgb(0,0,136)">int</span><span style=3D"color:rgb(102,102,0)">&gt;{</span>=
<span style=3D"color:rgb(0,102,102)">10</span><span style=3D"color:rgb(102,=
102,0)">,</span><span style=3D"color:rgb(0,0,0)"> </span><span style=3D"col=
or:rgb(0,102,102)">20</span><span style=3D"color:rgb(102,102,0)">});</span>=
</div></code></div><br>Let&#39;s assume that the sizes of these types are t=
he same. `pair` is TriviallyCopyable, as is `specific_bits`. So, is this we=
ll-defined behavior?<br><br>Well, `specific_bits` is technically just a pai=
r of integers, so it seems like it should be well-defined. However, it is m=
y understanding that it is essentially impossible to change the `i_` member=
 of any `specific_bits` object to any other value. At least, not from outsi=
de of the class; anything which would do so would run into UB at some point=
..<br></div></blockquote><div><br></div><div>Access control is not watertigh=
t enough for that. There are ways you can get your hands on a pointer to me=
mber to specific_bits::i_, at which point you can modify the field (<a href=
=3D"http://coliru.stacked-crooked.com/a/62dd7c4bfeb1eb96" rel=3D"nofollow" =
target=3D"_blank">http://coliru.stacked-crooked.com/a/62dd7c4bfeb1eb96</a>)=
..</div></div></blockquote><div><br></div><div>Why isn&#39;t `template struc=
t accessor&lt;specific_bits, &amp;specific_bits::i_&gt;;` a compile error? =
Doesn&#39;t it mention a name which is not accessible from that location?<b=
r></div></div></blockquote><div><br></div><div>Access checking is not appli=
ed to explicit instantiations (with exceptions not relevant here). See [tem=
p.spec]/6 in the current WP.=C2=A0</div></div>

<p></p>

-- <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 <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/e9d6281d-0256-437a-832f-62d2b885c552%=
40isocpp.org?utm_medium=3Demail&amp;utm_source=3Dfooter" target=3D"_blank">=
https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e9d6281d-0256-=
437a-832f-62d2b885c552%40isocpp.org</a>.<br>
</blockquote></div>

<p></p>

-- <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 <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CAB%2B4KHL-Kgk3rM2pujOs4yhrRw0EU8_7Hx=
%3DkiMqF-oRthj14MA%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter"=
>https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAB%2B4KHL-Kg=
k3rM2pujOs4yhrRw0EU8_7Hx%3DkiMqF-oRthj14MA%40mail.gmail.com</a>.<br />

--000000000000474ad5057f61a8b8--

.