220 29890 <20161217210804.GA10967@manuel-ThinkPad-L440.localdomain> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Manuel Bergler <berglerma@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: New smart pointer: CopyConstructible and
 CopyAssignable flavor of unique_ptr
Date: Sat, 17 Dec 2016 22:08:04 +0100
Lines: 150
Approved: news@gmane.org
Message-ID: <20161217210804.GA10967@manuel-ThinkPad-L440.localdomain>
References: <3df7e8eb-01a0-7804-f476-504b5a25a9fe@gmail.com>
 <CAFk2RUaY3+MnXkmvaC=LxgRA_RD_XQpFfX-HT6X3iq1wD7ggCg@mail.gmail.com>
 <20161217170428.GA10214@manuel-ThinkPad-L440.localdomain>
 <20161217171633.4919375.58072.21574@gmail.com>
 <CAEddoJaAZkeJS9Maj2z0vYGnfuNnJZ+qFFdjeQyesTF5itJxHA@mail.gmail.com>
 <15884364-3238-41c7-bc5a-aa5595a64ade@isocpp.org>
 <20161217182130.GA4347@manuel-ThinkPad-L440.localdomain>
 <5eae5f16-e129-4c95-8001-fc73cc4f3d03@isocpp.org>
 <20161217192812.GA17421@manuel-ThinkPad-L440.localdomain>
 <cb822c5c-fc50-48e5-ad60-fdb14580329c@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/signed; micalg=pgp-sha256;
	protocol="application/pgp-signature"; boundary="XsQoSWH+UP9D9v3l"
X-Trace: blaine.gmane.org 1482008933 32412 195.159.176.226 (17 Dec 2016 21:08:53 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Sat, 17 Dec 2016 21:08:53 +0000 (UTC)
User-Agent: Mutt/1.5.24 (2015-08-30)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDOZBAUFX4MRBZGS23BAKGQEL2OZ4EY@isocpp.org Sat Dec 17 22:08:49 2016
Return-path: <std-proposals+bncBDOZBAUFX4MRBZGS23BAKGQEL2OZ4EY@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-lf0-f69.google.com ([209.85.215.69])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDOZBAUFX4MRBZGS23BAKGQEL2OZ4EY@isocpp.org>)
	id 1cIMDo-0007eG-TC
	for gclcip-std-proposals@m.gmane.org; Sat, 17 Dec 2016 22:08:48 +0100
Original-Received: by mail-lf0-f69.google.com with SMTP id o20sf17575309lfg.2
        for <gclcip-std-proposals@m.gmane.org>; Sat, 17 Dec 2016 13:08:53 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:subject:message-id:references:mime-version
         :content-disposition:in-reply-to:user-agent:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=55ub9n1//ZuuSwbIDR2SSxSjxQpR+RZKhXHdt/PNWhQ=;
        b=R8pLZgLNL8IaS46ge3HK9wneRKCEAIcsjnOWkSTnoIzNprogpqJ3JVQdtKE+T+M7mP
         g5vptBhzCZOQ+fy0xtbysq+1jZOy4lcdKnO7d2t8Z4lGdXSufocOTR/udUtcFrd1SOE5
         mOXpx3pvonzXpilbggbfLkUC/XiSDi35UeWQwZHcAOtswWm7ORCGNLOa2CjxrRKKAzn9
         Tq0hBZ7J/oFt7IN3jgaX04lq3CJtDI8KdP0Gcnn7Pb30VScJAOxMCRkFxibgTabTSv9G
         EzuxWdaxhnDyuHBnzxEvkMAPTPtKQiWq4sBN17DE2u5l7EBRDqshMseyLK96i+kBauUk
         /Z 
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:subject:message-id:references
         :mime-version:content-disposition:in-reply-to:user-agent
         :x-original-sender:x-original-authentication-results:reply-to
         :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post
         :list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=55ub9n1//ZuuSwbIDR2SSxSjxQpR+RZKhXHdt/PNWhQ=;
        b=Ew+MVuaBr9VNx/CW+0YAQufFH0dZPO61arUFy35Ix9nm8nK/Zy81EzBi8tuI1e3MJ8
         4FF3iq8XomKWzXZ+w6o7NtMQrSX7dcvsMHkHpC/Q5I1bvnj0APxrq54gZtTDXMdhbeWG
         94owK8p3GqMi/X+p082R+gsZN68S5frJC71shpcvxoVxQZBPKoyeeyI5KjKDE4KN8daZ
         An0rqT20GtW0PlE1GkKtyQ+BU/XEpYBNg8MdNo3v9pdu0P0CYIvguiynHJN60BE0vohb
         3WO5Qp11UKdnKOxAx2gOzthH3WEhyxAFmVPlnQ+kfaXPi61EtCC7pWX1wbO738xaElZe
        
X-Gm-Message-State: AKaTC01DUilDfX0UvFzuuDqwfYFYV3hegsnxAxg8BX6LEl65dt2KlgA/FEW0KlNLKLRqnw==
X-Received: by 10.25.202.26 with SMTP id a26mr753903lfg.26.1482008933252;
        Sat, 17 Dec 2016 13:08:53 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.28.27.7 with SMTP id b7ls741309wmb.15.canary-gmail; Sat, 17
 Dec 2016 13:08:52 -0800 (PST)
X-Received: by 10.28.211.72 with SMTP id k69mr7780781wmg.138.1482008932007;
        Sat, 17 Dec 2016 13:08:52 -0800 (PST)
Original-Received: from mail-wj0-x244.google.com (mail-wj0-x244.google.com. [2a00:1450:400c:c01::244])
        by mx.google.com with ESMTPS id y124si9167394wme.83.2016.12.17.13.08.51
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Sat, 17 Dec 2016 13:08:51 -0800 (PST)
Received-SPF: pass (google.com: domain of berglerma@gmail.com designates 2a00:1450:400c:c01::244 as permitted sender) client-ip=2a00:1450:400c:c01::244;
Original-Received: by mail-wj0-x244.google.com with SMTP id he10so18845038wjc.2
        for <std-proposals@isocpp.org>; Sat, 17 Dec 2016 13:08:51 -0800 (PST)
X-Received: by 10.194.146.131 with SMTP id tc3mr8102629wjb.129.1482008931396;
        Sat, 17 Dec 2016 13:08:51 -0800 (PST)
Original-Received: from localhost (x5d856f73.dyn.telefonica.de. [93.133.111.115])
        by smtp.gmail.com with ESMTPSA id e6sm12959764wjw.33.2016.12.17.13.08.50
        for <std-proposals@isocpp.org>
        (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256);
        Sat, 17 Dec 2016 13:08:50 -0800 (PST)
Content-Disposition: inline
In-Reply-To: <cb822c5c-fc50-48e5-ad60-fdb14580329c@isocpp.org>
X-Original-Sender: berglerma@gmail.com
X-Original-Authentication-Results: mx.google.com;       dkim=pass
 header.i=@gmail.com;       spf=pass (google.com: domain of
 berglerma@gmail.com designates 2a00:1450:400c:c01::244 as permitted sender)
 smtp.mailfrom=berglerma@gmail.com;       dmarc=pass (p=NONE dis=NONE) header.from=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:29890
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/29890>


--XsQoSWH+UP9D9v3l
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Sat, Dec 17, 2016 at 12:35:03PM -0800, Nicol Bolas wrote:
>     It is different.
>
>     Consider for example something like
>
>     =C2=A0 =C2=A0 my_obj =3D std::accumulate(input.begin(), input.end(),
>     std::move(my_obj),
>     =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0some_function);
>
>     Afterwards references to my_obj will still be valid and correctly poi=
nt
>     to the
>     modified object.
>
>     On the other hand, if you have
>
>     =C2=A0 =C2=A0 auto f =3D [&someObj] (auto& acc, auto const& in) -> de=
cltype(auto) {
>     =C2=A0 =C2=A0 =C2=A0 =C2=A0 acc->doSomethingModifyingThePointeeObj(in=
);
>     =C2=A0 =C2=A0 =C2=A0 =C2=A0 someObj.doSomething(acc) =C2=A0// note I =
really want the cloning here
>     =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 // so I need to pass in t=
he clone_ptr
>     =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 // and not the pointee
>     =C2=A0 =C2=A0 =C2=A0 =C2=A0 return std::move(acc);
>     =C2=A0 =C2=A0 };
>
>     =C2=A0 =C2=A0 my_clone_ptr =3D std::accumulate(input.begin(), input.e=
nd(),
>     std::move(my_clone_ptr), f);
>
>     now suddenly all references you got by calling `*my_clone_ptr` somewh=
en
>     before
>     might or might not be invalid,
>
>   Actually, the standard guarantees that it isn't valid. Your
>   `move(my_clone_ptr)` will move-construct the `init` parameter of
>   `accumulate`. However, the standard says:
>
>   > Computes its result by initializing the accumulator acc with the init=
ial
>   value init
>
>   This means that it creates a new `T` called `acc`, which is initialized=
 by
>   the `init` value. Therefore, it will initialize `acc` by copy. `init` w=
ill
>   hold the original pointer until the end of `accumulate`, when it will b=
e
>   destroyed. What will be returned is `acc`, which is a copy of `init`.
>   =C2=A0

Apparently clang and gcc interpret this differently: They move it into plac=
e due to the std::move (see http://coliru.stacked-crooked.com/a/cbe39ffef2e=
bf716).

So in case of clang/gcc in my example the call to std::accumulate wouldn't =
invalidate existing references. But who knows, maybe some implementer of th=
e standard library will interpret the requirements the same way you did and=
 actually perform a copy. But only forgetting to `return std::move(acc);` i=
n the lambda used for `std::accumulate` and using just `return acc;` result=
s in a copy.

This is the entire point: When even very experienced people are not able to
predict what will happen, how is this a good feature? It is just too hard t=
o
use correctly.

>
>     even though I moved the original clone_ptr into
>     the algorithm to ensure that it doesn't get copied.
>
>   Replace `clone_ptr` with `any`. Or `vector`. Many types have this probl=
em
>   with `accumulate`; I don't see why adding one more will hurt anything.

This is because these types don't act as pointers; you're not supposed to s=
tore references to individual elements of a vector or a std::any, whereas t=
he interface of this copy_ptr is designed to do exactly that, even though i=
t is dangerous/unpredictable.

The only way to get around that issue is to not have additional references =
to the pointee, but then there is no need to have the smart pointer interfa=
ce and you can just use `indirect<T>`.

>
>   --
>   You received this message because you are subscribed to the Google Grou=
ps
>   "ISO C++ Standard - Future Proposals" group.
>   To unsubscribe from this group and stop receiving emails from it, send =
an
>   email to [1]std-proposals+unsubscribe@isocpp.org.
>   To post to this group, send email to [2]std-proposals@isocpp.org.
>   To view this discussion on the web visit
>   [3]https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/cb822c5=
c-fc50-48e5-ad60-fdb14580329c%40isocpp.org.
>
>References
>
>   Visible links
>   1. mailto:std-proposals+unsubscribe@isocpp.org
>   2. mailto:std-proposals@isocpp.org
>   3. https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/cb822c5=
c-fc50-48e5-ad60-fdb14580329c%40isocpp.org?utm_medium=3Demail&utm_source=3D=
footer

--=20
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 e=
mail 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/20161217210804.GA10967%40manuel-ThinkPad-L440.lo=
caldomain.

--XsQoSWH+UP9D9v3l
Content-Type: application/pgp-signature; name="signature.asc"

-----BEGIN PGP SIGNATURE-----

iQIzBAEBCAAdFiEEi3F6I6jTgMCHgGvofAJ3kFaTK3oFAlhVqTEACgkQfAJ3kFaT
K3p1hQ/+JLqh4xIB0WtYR+TNENu1DcJXcXDpuhX3kpNTow+joCLuDbic3HSFXchz
3s7WdTGGPmLpcyMtbBqWodgfIdE5ob/N6awY1M2guEJWIBXbhEVDy+C8M7xlfzMw
Iz5w7B41nEHww08V7xQg6JkcgSfGhmJ1ul8VbTmNNlAguTkCAM/ZeZFwtNz3ubjz
t1I0f+OcUVazwAS74UyDjvQdPxGHutmTCTbJiYgnvtabe6oMTdSceXdT5f+LIGvC
Cg8hT1Bl3NrVPIE1zjplLrdZW0DdjOY5rj9xHI1cymo73CRKRlVi/q82q3G0RXYx
8l1ljnpcVdklmGKgi/mQ+THBSkeZgtIF8r4JT6/Z28HAz4sMLr53lj87vOr/nU/Z
HkNNwHGLRH1l3WW2cor93gg/thibMqVYvuZZjKSByPhsUtQVcsC4LWi7wLgupteo
QaIw/8RJ5o0pThOK/RcFoLCVR14Yu+VoAH+X2+YhZoD6qcanlx3BNp8dVycie6PC
iMPq7VkPzxPZLzNCdx3jBlqay9i3gC0KksKoy0szvtL2YfarKR+UWQHmR/zUoMMS
cPW9ylESAi1yREIz4DuHCiC3gF3D5N4fskpoDx0WJWiy/lUgN2cWZI9iyrnQAdMr
uJkJU45z8+hClULaq0f4onWz8tBRkHw28arM4lJnPilabVhDbnw=
=KGYm
-----END PGP SIGNATURE-----

--XsQoSWH+UP9D9v3l--

.
