220 41350 <e9d6281d-0256-437a-832f-62d2b885c552@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: "T. C." <rs2740@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: `bit_cast` and when UB happens.
Date: Sun, 13 Jan 2019 17:53:02 -0800 (PST)
Lines: 186
Approved: news@gmane.org
Message-ID: <e9d6281d-0256-437a-832f-62d2b885c552@isocpp.org>
References: <752cc6a4-24d3-4e86-85a0-3ab0053008d7@isocpp.org>
 <0119543e-6d78-458a-af85-35f122a0b396@isocpp.org>
 <0c937122-b9e9-4258-a320-19fede2bfe22@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_1413_1306290076.1547430782531"
X-Trace: blaine.gmane.org 1547430658 11819 195.159.176.226 (14 Jan 2019 01:50:58 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Mon, 14 Jan 2019 01:50:58 +0000 (UTC)
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBCQ43G7NQIIRB76W57QQKGQEDQXLO5I@isocpp.org Mon Jan 14 02:50:54 2019
Return-path: <std-proposals+bncBCQ43G7NQIIRB76W57QQKGQEDQXLO5I@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-yb1-f199.google.com ([209.85.219.199])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBCQ43G7NQIIRB76W57QQKGQEDQXLO5I@isocpp.org>)
	id 1girOw-0002xA-6O
	for gclcip-std-proposals@m.gmane.org; Mon, 14 Jan 2019 02:50:54 +0100
Original-Received: by mail-yb1-f199.google.com with SMTP id f12sf10728695ybp.0
        for <gclcip-std-proposals@m.gmane.org>; Sun, 13 Jan 2019 17:53:05 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:message-id:in-reply-to:references:subject:mime-version
         :x-original-sender:reply-to:precedence:mailing-list:list-id
         :list-post:list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=uMzoOA8sryGXpyG0byXmwkVP1CX4bJZmp75xmbKAxds=;
        b=fsuTJAUqdBn/e4n+L2ACaYdtGRNuMfrIVTkHxLigTTueZr2jywacNr6sGLxIT5umEA
         62G9N2m7E//o4S5k8r5FTZcRxvQuyKz6DNiZdBmZRbmhuLePVN20R2JLPj0OgqWfc2kG
         a8JwikskNGNvLM0sciuB9WbbLVUoJO06/E1IUlQKDGNQ+VY1208royAkIYe5fd6ylkZt
         InSBo8Z8EnZvg4X5NAAZz3f8b1kKkfZgDY4DdPuaimn3WIvLetNN+LGmL2YDABGhsGx6
         tGCscNpsYCLsunkTN7tcw2xxO2jf3AZEBIg5CwWOuU7dKGlFAJkDi6ZwJYOhdhPwisr7
         2BxQ==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=date:from:to:message-id:in-reply-to:references:subject:mime-version
         :x-original-sender:reply-to:precedence:mailing-list:list-id
         :list-post:list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=uMzoOA8sryGXpyG0byXmwkVP1CX4bJZmp75xmbKAxds=;
        b=Wvy6O3T553Nz+4pK17p6yxzSJ3iRP3N9BrJOZOXCBgkoKqR3OxqygMIDV435yNkCM7
         +3SYwEWdvpmX26LLztNHJTth21R2MBnWwBIPxjCp0cmiXxyjR+2OFmq0I1KpYmTZ5ERN
         33z9FuCieSTd1CfJUXRPjSJzjW59cDFysAYRjCr/zgBgArKGrRJdbC86/yhIlt8gZp6/
         pNL17x0vIrh2/axp8eFS/B6NmquVecfI++isFw5ZsBa5iHOUoGjPGlHSR5MiAtAPR2Hr
         lcTFHoS51RmBasnzX3erQs7Togo8Wt0cZxIJFBGgzQxSL2Cz/KjifWiHs63zZczyr6bt
         mwBA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:date:from:to:message-id:in-reply-to:references
         :subject:mime-version:x-original-sender:reply-to:precedence
         :mailing-list:list-id:x-spam-checked-in-group:list-post:list-help
         :list-archive:list-subscribe:list-unsubscribe;
        bh=uMzoOA8sryGXpyG0byXmwkVP1CX4bJZmp75xmbKAxds=;
        b=d76Fw+kWmQvH0St4x2q4As6SWDT/K0zlSgzDVehA3uadpm9SYmJhfIOGorbice66Zp
         ti3yxLTbaWC4oSrr8vKpauFap/ORJF4KJXJU4MSZIlYSa+YkPAHvT2ojxs2ntiNMzwbM
         OIKdKPe3RoVnL3LUlkJree1lSBZTEGefN0yZ6zkshRsPnGssmb9aIc4HsK4GphLo9LNK
         /ugbdezVCW6RvkdiLb72cYx6xDIxpP8CrsqYVc8M1b/b71fJdyQrkrL4COrdTUsef0WK
         HaX+YFjKSh3/7uRZX8pXpIQYJUCpYuSSXXAb3mzOYdDkc3Z9BkelYBegWGiID0SVhKI4
         CEBQ==
X-Gm-Message-State: AJcUukcSFRz0EHkoCQ3STBBVrWGm7Uf9P48k566+2qXoCfltpLIXM++m
	k36nv1xRk/GSslannkNFbZy/Bw==
X-Google-Smtp-Source: ALg8bN5YFEy32EyhLaJxwtrKykK/kipZ3dJnlwxIATGLb+p7I5e0HAHvlJPcq2oJLz93gxbtmV5TLg==
X-Received: by 2002:a25:60c5:: with SMTP id u188mr4875327ybb.102.1547430784895;
        Sun, 13 Jan 2019 17:53:04 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 2002:a25:c042:: with SMTP id c63ls1739876ybf.4.gmail; Sun, 13 Jan
 2019 17:53:03 -0800 (PST)
X-Received: by 2002:a25:378a:: with SMTP id e132mr89748yba.0.1547430783349;
        Sun, 13 Jan 2019 17:53:03 -0800 (PST)
In-Reply-To: <0c937122-b9e9-4258-a320-19fede2bfe22@isocpp.org>
X-Original-Sender: rs2740@gmail.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Spam-Checked-In-Group: std-proposals@isocpp.org
X-Google-Group-Id: 399137483710
List-Post: <https://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <https://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <https://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:41350
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/41350>

------=_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--

.
