220 30413 <c6deb7b7-b65f-4327-80cd-d9fdbaf1d9ea@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Nicol Bolas <jmckesson@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: P0298: A byte type definition: with undefined
 pointer arithmetic?
Date: Sat, 7 Jan 2017 20:31:02 -0800 (PST)
Lines: 181
Approved: news@gmane.org
Message-ID: <c6deb7b7-b65f-4327-80cd-d9fdbaf1d9ea@isocpp.org>
References: <e21a1901-1ed5-5795-6244-dd70c4d08adf@f2.dion.ne.jp>
 <117168cf-8ab4-4048-aed6-38b68cb3ebca@isocpp.org>
 <d5d59214-e783-4d5c-bc34-1598d3eb34e9@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_9_1875176364.1483849862138"
X-Trace: blaine.gmane.org 1483849868 20030 195.159.176.226 (8 Jan 2017 04:31:08 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Sun, 8 Jan 2017 04:31:08 +0000 (UTC)
Cc: neilmac@microsoft.com
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBCEKFTV6ZUMBBBUBY7BQKGQEEOZWYFQ@isocpp.org Sun Jan 08 05:31:03 2017
Return-path: <std-proposals+bncBCEKFTV6ZUMBBBUBY7BQKGQEEOZWYFQ@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-it0-f69.google.com ([209.85.214.69])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBCEKFTV6ZUMBBBUBY7BQKGQEEOZWYFQ@isocpp.org>)
	id 1cQ58F-0004AR-Hu
	for gclcip-std-proposals@m.gmane.org; Sun, 08 Jan 2017 05:31:00 +0100
Original-Received: by mail-it0-f69.google.com with SMTP id n68sf58222578itn.4
        for <gclcip-std-proposals@m.gmane.org>; Sat, 07 Jan 2017 20:31:03 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:cc: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=4dYfNbcSYq8kTxytgztVlfqFUdvXpqF1zaOpPfhAjMM=;
        b=AfGUh5p5YLFW6Ll7F+HLBttH8lDKmZeu4K2e6cn7MwgtrcElEVTiL/V2qx/e4ZGw/J
         O1ZcBNIxCEMIh6oukJcwONw29vlm2w3WH6WXFq//A74SyAiSi/Q5UZUbboqefdRtcfzB
         gy4So6UgEcTJj7UCmxrQZ2+xbCmFBjqV5D+UbRSUlGAEIT9Bt6RMDK2AHDzzQDfJawfi
         lf52UGx5Bvxnw+P/ZybRBSS5hC4SDBzbI7ziqSiUzttpGZ84H7FEtJR76N+9qQlf3J28
         Ij/+meVo+PEjCHPKdR2rSM32P2S01GOkrAOSbQWQ+q1Z3Lpmuu7EHZB+zntv/AhydHJa
         xiGQ==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=date:from:to:cc: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=4dYfNbcSYq8kTxytgztVlfqFUdvXpqF1zaOpPfhAjMM=;
        b=NHWX2/OpWkPkcOUfbp0q5obD+NlE+9TXfrN/KwRyLC35acTdMYvD5c5vKW3NQUDWjB
         ELB1s1m/OBd7Ft/Oz98CRjnKR9IHJhXltOjaGfSZ5dXbA+pOMauf9QCf0Xu2QS/vnAcN
         mjZnANP1jmTvPp7jsWLi7RnPjAumLNtYeGqi9p+zsak6Czuv6o7OfCVuQ9xWDpAswae4
         LiMnXZcFafyCzgpwrigefn94D1n6Vd9tofQ3QBEZfhOXA8pQXdXkbQdLTvSiov9AjArb
         UKg4ZWSMzLhsQEB5+chBPGrzJw4VX8eiEWN4zBY5UD/dpujpgS/yxEDNzuyOpA8NK9Pq
         zCKA==
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:cc: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=4dYfNbcSYq8kTxytgztVlfqFUdvXpqF1zaOpPfhAjMM=;
        b=nulnNa93u1x1oD5Z8bwijCp9s3JnUaxm5y8+iR1UFbT3s4km+3+ldshmPvn7oWc19H
         m6qUnp7C8OhpfMFa0vxGfNKXbzNcvKOOtTpsegohP8kyeLONR0KjQtPF4l67W49HSdYe
         XZKc0d3npkWO4DM/4i3VRP/px4xXGqr295AIX961qgtViFEbpVmrI8ahoMBrOebbR7LO
         cEPtLXdCfLiR0pyzGqP74X2aCu/Fzc7XjXXz+rzmEKtt55BmdbR/KnKybTeG7YGPat5M
         NgRDz4dAHCF/P5Jm+ipc54V0t+LI+70hwNOvUPwRoQzTfmrXsBxCumV9kgYuq0lgYdKJ
         +ZQw==
X-Gm-Message-State: AIkVDXLVpNm+YbEDJ8X158VEkbLOdHwnBhGFt6dpk04vrSJZSWmtdeLAhN9DAeYUWhnP6w==
X-Received: by 10.36.103.2 with SMTP id u2mr1384161itc.30.1483849863367;
        Sat, 07 Jan 2017 20:31:03 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.157.46.77 with SMTP id c13ls4653208otd.49.gmail; Sat, 07 Jan
 2017 20:31:02 -0800 (PST)
X-Received: by 10.157.35.89 with SMTP id k25mr650393otd.11.1483849862778;
        Sat, 07 Jan 2017 20:31:02 -0800 (PST)
In-Reply-To: <d5d59214-e783-4d5c-bc34-1598d3eb34e9@isocpp.org>
X-Original-Sender: jmckesson@gmail.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <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:30413
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/30413>

------=_Part_9_1875176364.1483849862138
Content-Type: multipart/alternative; 
	boundary="----=_Part_10_449851246.1483849862138"

------=_Part_10_449851246.1483849862138
Content-Type: text/plain; charset=UTF-8

On Saturday, January 7, 2017 at 8:13:39 PM UTC-5, Chris Hallock wrote:
>
> On Saturday, January 7, 2017 at 3:07:23 PM UTC-8, Ryou wrote:
>>
>> [...]
>> So I think this is well-formed.
>>
>>     unsigned char storage[ sizeof(int) ] ;
>>     int * p1 = new(storage) int(42) ;
>>     unsigned char * p2 = reinterpret_cast<unsigned char *>(p1) ;
>>     p2 + 1 ; // OK
>>
>
> Agreed, except that, since the int object is not pointer-interconvertable 
> <http://eel.is/c++draft/basic.compound#4> to storage[0], you also need to 
> std::launder <http://eel.is/c++draft/ptr.launder> the result of the cast 
> under C++17.
>

I think this should be a defect in the pointer-interconvertibility rules. 
If an object B provides storage for A (in accord with [intro.object]/3), 
then they should be pointer-interconvertible, for the exact same reason as 
for member subobjects of unions and the first subobject of a standard 
layout struct.

This is also well-formed.
>>
>>     union { int i ; unsigned char buf[sizeof(int)] ; } u ;
>>     u.i = 42 ;
>>     unsigned char * p3 = reinterpret_cast< unsigned char *>( &u ) ;
>>     p3 + 1 ; // OK
>>
>
> This example also needs std::launder, 
>

A union is pointer-interconvertible with *all* of its member subobjects, 
per [basic.compound]/4:

> one is a standard-layout union object and the other is a non-static data 
member of that object

So laundering is not needed. You're right about the rest, though.

but even with that, p3 would only point to the first byte in the object 
> representation of u (or, equivalently, u.i), not u.buf[0]. u.buf[0] 
> doesn't exist (as a live object). It doesn't exist because u.buf itself 
> doesn't exist, and that's because it was never made the active member. 
> There's only u.i. Since there is no live array object, the last line is 
> strictly undefined under the current (but presumably defective) wording.
>
>
> But this one is undefined behaviour?
>>
>>     int i = 42 ;
>>     unsigned char * p4 = reinterpret_cast< unsigned char *>( &i ) ;
>>     p4 + 1 ; // Undefined behaviour?
>>
>> Because the storage for object i is not associated with array of unsigned 
>> char. 
>>
>
> Yes, under the current (but presumably defective) wording. (Also, this 
> needs std::launder.)
>

Personally, I don't think any of those *should* need `std::launder`. 
Byte-wise access to an object has nothing to do with the purposes that 
`launder` was invented to solve.

-- 
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/c6deb7b7-b65f-4327-80cd-d9fdbaf1d9ea%40isocpp.org.

------=_Part_10_449851246.1483849862138
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Saturday, January 7, 2017 at 8:13:39 PM UTC-5, Chris Ha=
llock wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-lef=
t: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">O=
n Saturday, January 7, 2017 at 3:07:23 PM UTC-8, Ryou wrote:<blockquote cla=
ss=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc=
 solid;padding-left:1ex"><div dir=3D"ltr">[...]<br><div>So I think this is =
well-formed.</div><div><br></div><div><div>=C2=A0 =C2=A0 unsigned char stor=
age[ sizeof(int) ] ;</div><div>=C2=A0 =C2=A0 int * p1 =3D new(storage) int(=
42) ;</div><div>=C2=A0 =C2=A0 unsigned char * p2 =3D reinterpret_cast&lt;un=
signed char *&gt;(p1) ;</div><div>=C2=A0 =C2=A0 p2 + 1 ; // OK</div></div><=
/div></blockquote><div><br>Agreed, except that, since the <span style=3D"fo=
nt-family:courier new,monospace">int</span> object is not <a href=3D"http:/=
/eel.is/c++draft/basic.compound#4" target=3D"_blank" rel=3D"nofollow" onmou=
sedown=3D"this.href=3D&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Feel.=
is%2Fc%2B%2Bdraft%2Fbasic.compound%234\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAF=
QjCNFdUq4Tnm-sQBVdpKIgsyCGNf2Tig&#39;;return true;" onclick=3D"this.href=3D=
&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Feel.is%2Fc%2B%2Bdraft%2Fba=
sic.compound%234\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFdUq4Tnm-sQBVdpKIg=
syCGNf2Tig&#39;;return true;">pointer-interconvertable</a> to <span style=
=3D"font-family:courier new,monospace">storage[0]</span>, you also need to =
<a href=3D"http://eel.is/c++draft/ptr.launder" target=3D"_blank" rel=3D"nof=
ollow" onmousedown=3D"this.href=3D&#39;http://www.google.com/url?q\x3dhttp%=
3A%2F%2Feel.is%2Fc%2B%2Bdraft%2Fptr.launder\x26sa\x3dD\x26sntz\x3d1\x26usg\=
x3dAFQjCNE3gE82aH27Bz_fvgvMTdRh5wmtZg&#39;;return true;" onclick=3D"this.hr=
ef=3D&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Feel.is%2Fc%2B%2Bdraft=
%2Fptr.launder\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNE3gE82aH27Bz_fvgvMTd=
Rh5wmtZg&#39;;return true;"><span style=3D"font-family:courier new,monospac=
e">std::launder</span></a> the result of the cast under C++17.<br></div></d=
iv></blockquote><div><br>I think this should be a defect in the pointer-int=
erconvertibility rules. If an object B provides storage for A (in accord wi=
th [intro.object]/3), then they should be pointer-interconvertible, for the=
 exact same reason as for member subobjects of unions and the first subobje=
ct of a standard layout struct.<br><br></div><blockquote class=3D"gmail_quo=
te" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;paddi=
ng-left: 1ex;"><div dir=3D"ltr"><blockquote class=3D"gmail_quote" style=3D"=
margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex"><di=
v dir=3D"ltr"><div></div><div>This is also well-formed.</div><div><br></div=
><div><div>=C2=A0 =C2=A0 union { int i ; unsigned char buf[sizeof(int)] ; }=
 u ;</div><div>=C2=A0 =C2=A0 u.i =3D 42 ;</div><div>=C2=A0 =C2=A0 unsigned =
char * p3 =3D reinterpret_cast&lt; unsigned char *&gt;( &amp;u ) ;</div><di=
v>=C2=A0 =C2=A0 p3 + 1 ; // OK</div></div></div></blockquote><div><br>This =
example also needs <span style=3D"font-family:courier new,monospace">std::l=
aunder</span>, </div></div></blockquote><div><br>A union is pointer-interco=
nvertible with <i>all</i> of its member subobjects, per [basic.compound]/4:=
<br><br>&gt; one is a standard-layout union object and the other is a non-s=
tatic data member of that object<br><br>So laundering is not needed. You&#3=
9;re right about the rest, though.<br><br></div><blockquote class=3D"gmail_=
quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;pa=
dding-left: 1ex;"><div dir=3D"ltr"><div>but even with that, <span style=3D"=
font-family:courier new,monospace">p3</span> would only point to the first =
byte in the object representation of <span style=3D"font-family:courier new=
,monospace">u</span> (or, equivalently, <span style=3D"font-family:courier =
new,monospace">u.i</span>), not <span style=3D"font-family:courier new,mono=
space">u.buf[0]</span>. <span style=3D"font-family:courier new,monospace">u=
..buf[0]</span> doesn&#39;t exist (as a live object). It doesn&#39;t exist b=
ecause <span style=3D"font-family:courier new,monospace">u.buf</span> itsel=
f doesn&#39;t exist, and that&#39;s because it was never made the active me=
mber. There&#39;s only <span style=3D"font-family:courier new,monospace">u.=
i</span>. Since there is no live array object, the last line is strictly un=
defined under the current (but presumably defective) wording.<br><br><br></=
div><blockquote class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;b=
order-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div></div><di=
v>But this one is undefined behaviour?</div><div><br></div><div><div>=C2=A0=
 =C2=A0 int i =3D 42 ;</div><div>=C2=A0 =C2=A0 unsigned char * p4 =3D reint=
erpret_cast&lt; unsigned char *&gt;( &amp;i ) ;</div><div>=C2=A0 =C2=A0 p4 =
+ 1 ; // Undefined behaviour?</div></div><div><br></div><div>Because the st=
orage for object i is not associated with array of unsigned char.=C2=A0</di=
v></div></blockquote><div><br>Yes, under the current (but presumably defect=
ive) wording. (Also, this needs <span style=3D"font-family:courier new,mono=
space">std::launder</span>.)<br></div></div></blockquote><div><br>Personall=
y, I don&#39;t think any of those <i>should</i> need `std::launder`. Byte-w=
ise access to an object has nothing to do with the purposes that `launder` =
was invented to solve.<br></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/c6deb7b7-b65f-4327-80cd-d9fdbaf1d9ea%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/c6deb7b7-b65f-4327-80cd-d9fdbaf1d9ea=
%40isocpp.org</a>.<br />

------=_Part_10_449851246.1483849862138--

------=_Part_9_1875176364.1483849862138--

.
