220 36329 <36d5cdb5-0213-4efd-9d36-8179177f604b@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: Re: Trivial copyability and `optiona/variant`.
Date: Sun, 24 Dec 2017 20:15:39 -0800 (PST)
Lines: 150
Approved: news@gmane.org
Message-ID: <36d5cdb5-0213-4efd-9d36-8179177f604b@isocpp.org>
References: <f8ccbc4e-d67c-4af5-933a-0d2822622baa@isocpp.org> <390b06f2-0c86-465e-9a11-d0ebb3501970@isocpp.org>
 <CAFk2RUY8UDDb2NXs0FdPCbCUYvzLmjYfu_GkU3FgzPiiOXX+TQ@mail.gmail.com>
 <0f45770f-dc62-4459-8e1c-6aece46d05e6@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_14501_1636509730.1514175339816"
X-Trace: blaine.gmane.org 1514175229 13143 195.159.176.226 (25 Dec 2017 04:13:49 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Mon, 25 Dec 2017 04:13:49 +0000 (UTC)
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBCEKFTV6ZUMBB3HWQHJAKGQEP3HOTVY@isocpp.org Mon Dec 25 05:13:45 2017
Return-path: <std-proposals+bncBCEKFTV6ZUMBB3HWQHJAKGQEP3HOTVY@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-ua0-f199.google.com ([209.85.217.199])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBCEKFTV6ZUMBB3HWQHJAKGQEP3HOTVY@isocpp.org>)
	id 1eTK8x-0002eU-Q7
	for gclcip-std-proposals@m.gmane.org; Mon, 25 Dec 2017 05:13:39 +0100
Original-Received: by mail-ua0-f199.google.com with SMTP id h30sf21065834uac.1
        for <gclcip-std-proposals@m.gmane.org>; Sun, 24 Dec 2017 20:15:42 -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=IshuzOqh9RjAU2oFNO8Rvggwcwl+XcHj5qYhJrrHtFw=;
        b=BH/ax04PLfKEh297YbxAM2O1bKFlmShUlQpmjt94VpCEAJ++y9DOnWNi61F6zXdZCZ
         nbrhzvMcPOFth6FINCInDXbFQ3pd7a9M2T+1/PB7AwUFGwStqLimMlkVmRllZTpGzciZ
         GGO9nib86UasW1PLdt+1Cv3NSfdFtg7tdM+O6C6m8WG2QBPF97sspQF6enVMPXERjXNI
         Usa1SuUVkrMeZSiG+y+UdXjO31/72pB2fNXDDBy5cxHQguys1bbiBmt8KR9L0gUN+rNv
         OAAbfZxa5qrFO+hf/M+vqhxZjiFZ+uZ9YoiI4M9MzS2CFb9HgX5sW8Mgv3BqSgAbRi2e
         aGww==
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=IshuzOqh9RjAU2oFNO8Rvggwcwl+XcHj5qYhJrrHtFw=;
        b=ILKs46z6C7EjG7sNWVwMzW0qqDWS6IjYidA4ZRcnOj8pfL/HPdM5Q7lCNAQG33ESr0
         NgPG7D/CUTSMZCzmuSljjbGEMPZ0V15ZLFKotJ8Uycw5sxoGiPxVtOMd1AQzGYXMzpCS
         /oA2JZC0MSSXF8r5W431mgEr5HaFb5nWdxzBHbI56H/Ln2bf+dy4D8VrsGZWqq1MY865
         yLU5vF64+gpE8/UHplpRAFvaD3cT51DSy5rymsnNFc/YEIED2zoRsVdtGurtNYzyCfgj
         0z6qqp8MNF3RiTWZG7LYSAPd5PnkH2YhoPQDxrrkr7GHm5CMcz3wZakw+QvQwjsWiEwA
         oxgw==
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=IshuzOqh9RjAU2oFNO8Rvggwcwl+XcHj5qYhJrrHtFw=;
        b=h0fY5D809fPF5XyS1zxiP0aLjU4asK3rfYdcAK6g65/zsuGPAfsKN7GBwnhiGcJ9c+
         fM/o1MWqKCO2jKn4SUzyVdOyplM2ExpzyVrgNeBYC9YpqqYmjJZAxBq3JCZN2omzfefa
         FvtYg5ytmVhojZLQfwrMBiMP7YvWz/H5p6uBHmeMei2I4lBbZRXVumAwpHnmBH2j94UO
         TeksYZggkgJq+SNw17m38uFVy8rkYqTeadL4ncWAkBJX07unjhnEd4j518SopHwfbrpR
         AioFkIms6kBbo1E1ajfRyB14O6/i9N9Ms6bRj6rtm3kAf9OjuBvGbl87ULBq5lLyJWhk
         6TkQ==
X-Gm-Message-State: AKGB3mLQNYzFX8Z94MEm616GDNnvYZtAc7wDOolXsxEVhosbzf0F868q
	L0iaKhJhZuI5YNLMm1uq7atgeg==
X-Google-Smtp-Source: ACJfBos3B10tx/3T2yFB+0Zyj6pu9eDSOIBwXuRELjAFoO3ylH1RE+xcsSz+BY/hcymktTojJlofJQ==
X-Received: by 10.176.32.17 with SMTP id v17mr11298152uak.0.1514175342179;
        Sun, 24 Dec 2017 20:15:42 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.176.96.26 with SMTP id j26ls2759366ual.1.gmail; Sun, 24 Dec
 2017 20:15:40 -0800 (PST)
X-Received: by 10.31.61.149 with SMTP id k143mr1963513vka.7.1514175340506;
        Sun, 24 Dec 2017 20:15:40 -0800 (PST)
In-Reply-To: <0f45770f-dc62-4459-8e1c-6aece46d05e6@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-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:36329
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/36329>

------=_Part_14501_1636509730.1514175339816
Content-Type: multipart/alternative; 
	boundary="----=_Part_14502_392247051.1514175339817"

------=_Part_14502_392247051.1514175339817
Content-Type: text/plain; charset="UTF-8"



On Sunday, December 24, 2017 at 8:40:14 PM UTC-5, Arthur O'Dwyer wrote:
>
> On Saturday, December 23, 2017 at 4:47:05 AM UTC-8, Ville Voutilainen 
> wrote:
>>
>> On 23 December 2017 at 14:17, Andrey Davydov <andrey.a...@gmail.com> 
>> wrote: 
>> > Is there any progress being made on this? As I see now the Microsoft 
>> and 
>> > libc++ implementation maintain trivial copyability, but libstdc++ 
>> doesn't: 
>> > https://godbolt.org/g/tL29Hd 
>>
>> The proposal for that hasn't apparently been adopted yet, so this 
>> hasn't popped up as a high-priority 
>> item on the libstdc++ todo list. I suppose I should implement it 
>> anyway, but I can't make any promises 
>> that it'll make it into gcc 8.
>>
>
> Coincidentally, "trivial copyability of std::optional" just popped up over 
> on the SG14 list as well:
>
> https://groups.google.com/a/isocpp.org/forum/#!msg/sg14/ZqrAbZ2Vq9s/iNVCL3naDAAJ
>
> The fundamental problem (IMO) is that if you make optional<T> trivially 
> copyable, then you have
>
>     optional<int> a = 42;  // correctly constructs an `int` and begins its 
> lifetime
>     optional<int> b = a;  // trivially copies some bits into the memory 
> address of `b`
>
> In the `b` case, there is no `int` object actually constructed (assuming 
> the common implementation of `optional<T>` as containing a union of a dummy 
> char and a `T` object, where the `T` object is not constructed by default). 
> So the first access to `b.value()` invokes undefined behavior, as far as 
> I'm aware.
>

That would basically mean that any trivially copyable `union` type cannot 
be copied at all. I don't buy that the standard says that. Indeed, it very 
much does not [basic.types]/3:

> For any trivially copyable type T, if two pointers to T point to distinct 
T objects obj1 and obj2, where neither obj1 nor obj2 is a base-class 
subobject, if the underlying bytes (4.4) making up obj1 are copied into 
obj2 , 44 obj2 shall subsequently hold the same value as obj1.

And that's just for doing a memcpy-based copy assignment.

For a `union`, to "hold the same value" as another instance of that union 
also means to have active the same member subobject as the other `union`. 
Do you have some specific statement in the standard that says otherwise?

-- 
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/36d5cdb5-0213-4efd-9d36-8179177f604b%40isocpp.org.

------=_Part_14502_392247051.1514175339817
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>On Sunday, December 24, 2017 at 8:40:14 PM UTC-5, =
Arthur O&#39;Dwyer 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">On Saturday, December 23, 2017 at 4:47:05 AM UTC-8, Ville Vouti=
lainen wrote:<blockquote class=3D"gmail_quote" style=3D"margin:0;margin-lef=
t:0.8ex;border-left:1px #ccc solid;padding-left:1ex">On 23 December 2017 at=
 14:17, Andrey Davydov &lt;<a rel=3D"nofollow">andrey.a...@gmail.com</a>&gt=
; wrote:
<br>&gt; Is there any progress being made on this? As I see now the Microso=
ft and
<br>&gt; libc++ implementation maintain trivial copyability, but libstdc++ =
doesn&#39;t:
<br>&gt; <a onmousedown=3D"this.href=3D&#39;https://www.google.com/url?q\x3=
dhttps%3A%2F%2Fgodbolt.org%2Fg%2FtL29Hd\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dA=
FQjCNGfgkB2cXDRCoD6cMOuz6RimBAS-g&#39;;return true;" onclick=3D"this.href=
=3D&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgodbolt.org%2Fg%2FtL2=
9Hd\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGfgkB2cXDRCoD6cMOuz6RimBAS-g&#3=
9;;return true;" href=3D"https://godbolt.org/g/tL29Hd" target=3D"_blank" re=
l=3D"nofollow">https://godbolt.org/g/tL29Hd</a>
<br>
<br>The proposal for that hasn&#39;t apparently been adopted yet, so this
<br>hasn&#39;t popped up as a high-priority
<br>item on the libstdc++ todo list. I suppose I should implement it
<br>anyway, but I can&#39;t make any promises
<br>that it&#39;ll make it into gcc 8.<br></blockquote><div><br></div><div>=
Coincidentally, &quot;trivial copyability of std::optional&quot; just poppe=
d up over on the SG14 list as well:</div><div><a onmousedown=3D"this.href=
=3D&#39;https://groups.google.com/a/isocpp.org/forum/#!msg/sg14/ZqrAbZ2Vq9s=
/iNVCL3naDAAJ&#39;;return true;" onclick=3D"this.href=3D&#39;https://groups=
..google.com/a/isocpp.org/forum/#!msg/sg14/ZqrAbZ2Vq9s/iNVCL3naDAAJ&#39;;ret=
urn true;" href=3D"https://groups.google.com/a/isocpp.org/forum/#!msg/sg14/=
ZqrAbZ2Vq9s/iNVCL3naDAAJ" target=3D"_blank" rel=3D"nofollow">https://groups=
..google.com/a/<wbr>isocpp.org/forum/#!msg/sg14/<wbr>ZqrAbZ2Vq9s/iNVCL3naDAA=
J</a><br></div><div><br></div><div>The fundamental problem (IMO) is that if=
 you make optional&lt;T&gt; trivially copyable, then you have</div><div><br=
></div><div>=C2=A0 =C2=A0 optional&lt;int&gt; a =3D 42; =C2=A0// correctly =
constructs an `int` and begins its lifetime</div><div>=C2=A0 =C2=A0 optiona=
l&lt;int&gt; b =3D a; =C2=A0// trivially copies some bits into the memory a=
ddress of `b`</div><div><br></div><div>In the `b` case, there is no `int` o=
bject actually constructed (assuming the common implementation of `optional=
&lt;T&gt;` as containing a union of a dummy char and a `T` object, where th=
e `T` object is not constructed by default). So the first access to `b.valu=
e()` invokes undefined behavior, as far as I&#39;m aware.</div></div></bloc=
kquote><div><br></div><div>That would basically mean that any trivially cop=
yable `union` type cannot be copied at all. I don&#39;t buy that the standa=
rd says that. Indeed, it very much does not [basic.types]/3:</div><div><br>=
</div><div>&gt;=C2=A0For any trivially copyable type T, if two pointers to =
T point to distinct T objects obj1 and obj2, where neither obj1 nor obj2 is=
 a base-class subobject, if the underlying bytes (4.4) making up obj1 are c=
opied into obj2 , 44 obj2 shall subsequently hold the same value as obj1.</=
div><div><br></div><div>And that&#39;s just for doing a memcpy-based copy a=
ssignment.</div><div><br></div><div>For a `union`, to &quot;hold the same v=
alue&quot; as another instance of that union also means to have active the =
same member subobject as the other `union`. Do you have some specific state=
ment in the standard that says otherwise?</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/36d5cdb5-0213-4efd-9d36-8179177f604b%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/36d5cdb5-0213-4efd-9d36-8179177f604b=
%40isocpp.org</a>.<br />

------=_Part_14502_392247051.1514175339817--

------=_Part_14501_1636509730.1514175339816--

.
