220 30412 <d5d59214-e783-4d5c-bc34-1598d3eb34e9@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Chris Hallock <christopherhallock@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 17:13:39 -0800 (PST)
Lines: 128
Approved: news@gmane.org
Message-ID: <d5d59214-e783-4d5c-bc34-1598d3eb34e9@isocpp.org>
References: <e21a1901-1ed5-5795-6244-dd70c4d08adf@f2.dion.ne.jp>
 <117168cf-8ab4-4048-aed6-38b68cb3ebca@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_2349_1282421112.1483838019427"
X-Trace: blaine.gmane.org 1483838024 12795 195.159.176.226 (8 Jan 2017 01:13:44 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Sun, 8 Jan 2017 01:13:44 +0000 (UTC)
Cc: neilmac@microsoft.com
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBCHINCHEQEFBBRFEY3BQKGQEALED6FY@isocpp.org Sun Jan 08 02:13:39 2017
Return-path: <std-proposals+bncBCHINCHEQEFBBRFEY3BQKGQEALED6FY@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-io0-f199.google.com ([209.85.223.199])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBCHINCHEQEFBBRFEY3BQKGQEALED6FY@isocpp.org>)
	id 1cQ23F-0002SE-8o
	for gclcip-std-proposals@m.gmane.org; Sun, 08 Jan 2017 02:13:37 +0100
Original-Received: by mail-io0-f199.google.com with SMTP id v96sf6243364ioi.5
        for <gclcip-std-proposals@m.gmane.org>; Sat, 07 Jan 2017 17:13:41 -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=zx2Qrq7TLt9BWlt9t1oPYuAcFxgEZTDAFC3DJCsPhkc=;
        b=RevVEri7mQoW5WSBq2Wpzc7ATYRFjDIWPz8mdB+S2NwBXP2MuyciX9XtHm6cBBTaNr
         y+nUNX6FYYeTxWkwIcC8ULHvi/GT/1ujA8MwQBVpO8KreyTbpHlKARNAcPQMD+Oplswe
         YaDswfKderGLsyljp+drlCFBl/9EqKAx5y6JgnZ9DQwT6FgtEhegozqSPP+wgQZ+IFTr
         yTk/0LEI8xNUPhwDbQPxBXOsOy506mEzcvybRDhsr/EBzFZp8dG+TSlWCULdycAK91X3
         83P18ccgBtjIB4jBPAOVVk1gVLOFSOntg3sz7QFDjLMBx4tVJQ+j/4duioMwjR2FrLUF
         q1lQ==
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=zx2Qrq7TLt9BWlt9t1oPYuAcFxgEZTDAFC3DJCsPhkc=;
        b=Lq+bfDZTKTdPszrxWi10Uy5/IcLe+MFihcV28yCyKG7lOdodS2q9PkEV+G3qtLBpOQ
         uCLNr1AsRLUcjEy82cbdNHhMVTMEIp8NdcEzpWlWsBTUdaD4OBkjjWRBXSSKGbcBvZeh
         XWw1sBdoPKomi2dxzS21KiV2lB4luHr9EIGouFZkAJknPodq8zwQxyTUJ8LFiu282R7C
         RJzta0goskGJKjVGrDZ2T6/diM2k4FuDO6pJ/Ld41KxcE3iyLGRkxrveOmnFB9SQ6QFe
         Anakixx+3WQ7t53QO5pl8MhPQv2YiwfG4lKJB4SmjHBOgrH1UKkyyjgz7OuB2gWenHDb
         JVQw==
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=zx2Qrq7TLt9BWlt9t1oPYuAcFxgEZTDAFC3DJCsPhkc=;
        b=VVZIyFGqrqL5IiSa70LaP/swHLW/8IRSmRJum2UlfIQOyJt1TAD3ykUBecOb9Mwf5e
         2dffWKp7D8hfemEG8I1QQygb+TaUjKoqwhXV8LoCMnu5YgGVFTrSX5AMD2OU25Nb5dpa
         8OmhB8jtj3SdtOyj5zlvec/8XK/pr2eoaolE4isN0Vi/kd2kjIriooDUl9I+f6aCmQmb
         WoLlhpZy/sBI2aprtwyw7rtMCa9ikVktAKLrnH/yC3BLoN64gcLWEsV6wAUTDaKLwZul
         woTGoAcU9D6r1JcQCzQosNMvyAtjVxGKHTyukhnqg/LLhtonTE67ejMUP4Rx5hHCVil/
         rhGw==
X-Gm-Message-State: AIkVDXLIaSPGp8cS70vINkho1xfk8bMjZKyZxx8H55FgiPw6d6NqT8zIvvCiSFu7QTvqAg==
X-Received: by 10.107.156.78 with SMTP id f75mr3527820ioe.86.1483838020926;
        Sat, 07 Jan 2017 17:13:40 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.157.17.12 with SMTP id g12ls4349886ote.26.gmail; Sat, 07 Jan
 2017 17:13:40 -0800 (PST)
X-Received: by 10.157.8.134 with SMTP id 6mr638868otf.17.1483838020125;
        Sat, 07 Jan 2017 17:13:40 -0800 (PST)
In-Reply-To: <117168cf-8ab4-4048-aed6-38b68cb3ebca@isocpp.org>
X-Original-Sender: christopherhallock@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:30412
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/30412>

------=_Part_2349_1282421112.1483838019427
Content-Type: multipart/alternative; 
	boundary="----=_Part_2350_1731349889.1483838019427"

------=_Part_2350_1731349889.1483838019427
Content-Type: text/plain; charset=UTF-8

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.


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, 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.)

-- 
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/d5d59214-e783-4d5c-bc34-1598d3eb34e9%40isocpp.org.

------=_Part_2350_1731349889.1483838019427
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Saturday, January 7, 2017 at 3:07:23 PM UTC-8, Ryou wro=
te:<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">[...]<br><=
div>So I think this is well-formed.</div><div><br></div><div><div>=C2=A0 =
=C2=A0 unsigned char storage[ 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;unsigned char *&gt;(p1) ;</div><div>=C2=A0 =C2=A0 p=
2 + 1 ; // OK</div></div></div></blockquote><div><br>Agreed, except that, s=
ince the <span style=3D"font-family: courier new,monospace;">int</span> obj=
ect is not <a href=3D"http://eel.is/c++draft/basic.compound#4">pointer-inte=
rconvertable</a> to <span style=3D"font-family: courier new,monospace;">sto=
rage[0]</span>, you also need to <a href=3D"http://eel.is/c++draft/ptr.laun=
der"><span style=3D"font-family: courier new,monospace;">std::launder</span=
></a> the result of the cast under C++17.<br><br><br></div><blockquote clas=
s=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #c=
cc solid;padding-left: 1ex;"><div dir=3D"ltr"><div></div><div>This is also =
well-formed.</div><div><br></div><div><div>=C2=A0 =C2=A0 union { int i ; un=
signed 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><div>=C2=A0 =C2=A0 p3 + 1 ; // OK</div></div><=
/div></blockquote><div><br>This example also needs <span style=3D"font-fami=
ly: courier new,monospace;">std::launder</span>, 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-fa=
mily: courier new,monospace;">u.i</span>), not <span style=3D"font-family: =
courier new,monospace;">u.buf[0]</span>. <span style=3D"font-family: courie=
r new,monospace;">u.buf[0]</span> doesn&#39;t exist (as a live object). It =
doesn&#39;t exist because <span style=3D"font-family: courier new,monospace=
;">u.buf</span> itself doesn&#39;t exist, and that&#39;s because it was nev=
er made the active member. There&#39;s only <span style=3D"font-family: cou=
rier new,monospace;">u.i</span>. Since there is no live array object, the l=
ast line is strictly undefined under the current (but presumably defective)=
 wording.<br><br><br></div><blockquote class=3D"gmail_quote" style=3D"margi=
n: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><di=
v dir=3D"ltr"><div></div><div>But this one is undefined behaviour?</div><di=
v><br></div><div><div>=C2=A0 =C2=A0 int i =3D 42 ;</div><div>=C2=A0 =C2=A0 =
unsigned char * p4 =3D reinterpret_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 storage for object i is not associated with array=
 of unsigned char.=C2=A0</div></div></blockquote><div><br>Yes, under the cu=
rrent (but presumably defective) wording. (Also, this needs <span style=3D"=
font-family: courier new,monospace;">std::launder</span>.)<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/d5d59214-e783-4d5c-bc34-1598d3eb34e9%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/d5d59214-e783-4d5c-bc34-1598d3eb34e9=
%40isocpp.org</a>.<br />

------=_Part_2350_1731349889.1483838019427--

------=_Part_2349_1282421112.1483838019427--

.
