220 12384 <CA+cyFgsgARGT6WTyFwRDwtNZGEBPxw2KKiQywxvPJ29sJK+U3Q@mail.gmail.com> article
Path: news.gmane.org!not-for-mail
From: "'Geoffrey Romer' via ISO C++ Standard - Future Proposals" <std-proposals@isocpp.org>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Re: Local variables that overstay their welcome
Date: Wed, 20 Aug 2014 11:44:06 -0700
Lines: 186
Approved: news@gmane.org
Message-ID: <CA+cyFgsgARGT6WTyFwRDwtNZGEBPxw2KKiQywxvPJ29sJK+U3Q@mail.gmail.com>
References: <778b6fbf-3b58-488c-9e51-32a05b95831e@isocpp.org>
	<b9004175-65ac-4a4a-847e-a474499b1e3d@isocpp.org>
	<e26c6071-2a98-4ca3-89d1-006e240e5a30@isocpp.org>
	<CAD6_Qj9MFG2J9UuwH_gtoKNWAEXCwirikxYF9PFRGD_8uErVgQ@mail.gmail.com>
	<c2355b21-614c-415e-8bb1-60f2333a9ed0@isocpp.org>
	<CAD6_Qj-pd6qt-sYCNiCqk7KemGtq5xrXMkyqddF=ttENb02vbQ@mail.gmail.com>
	<75655d78-f533-40fe-8109-2725a5da77a9@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=001a11c2cc1825d2f5050113fd68
X-Trace: ger.gmane.org 1408560259 13378 80.91.229.3 (20 Aug 2014 18:44:19 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Wed, 20 Aug 2014 18:44:19 +0000 (UTC)
Cc: dibeas@ieee.org
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBD74DCN5SYHBB56Y2OPQKGQEJHHBRTY@isocpp.org Wed Aug 20 20:44:12 2014
Return-path: <std-proposals+bncBD74DCN5SYHBB56Y2OPQKGQEJHHBRTY@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-ob0-f197.google.com ([209.85.214.197])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBD74DCN5SYHBB56Y2OPQKGQEJHHBRTY@isocpp.org>)
	id 1XKArg-0000fw-J5
	for gclcip-std-proposals@m.gmane.org; Wed, 20 Aug 2014 20:44:08 +0200
Original-Received: by mail-ob0-f197.google.com with SMTP id vb8sf50950034obc.8
        for <gclcip-std-proposals@m.gmane.org>; Wed, 20 Aug 2014 11:44:07 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:mime-version:in-reply-to:references:date
         :message-id:subject:from:to:cc:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe:content-type;
        bh=gVJq4+DmSgVsa+iQYVaf2WDBiyimIWsS5DemTp5lpT8=;
        b=dB3Gmtbn13EnXz8O/VX/WUrB8/uFURkEi90POMgaZLPrw1rYLFfnF+W/yKcIUj2+aN
         CSfi0nb3jd3OLHcWZJMaXjMy3Kuxqm8UM5viY7hS7eIgKYPz0uEy61vIzOQ5/NdB1i6C
         DN72MjzIkg0Y+eEj6mqIISlUbxKvMI4ERY0LPw3E2qieVoswRGyWRiVUk63R9z80gGfq
         XgfjawLxMv8eRPz9EQGBqhSzHYgXmNTo34f5ei89hdClJ4NTEpknqF9CZjaUEmiOPSd8
         0K5KYLjiVYJ3wN9FGeEd6v6fqz7ZtoFROMCtR0jEOWAdzLvNZeD0LBsMhSIZ+D8F0XHS
         w6Sg==
X-Gm-Message-State: ALoCoQm03I7uPtDEgZckhy55IzX2P9fSOWj2Rmg9FsF9aRflJFaFxv3QYNNO/z8mfL6Xs46tMYCd
X-Received: by 10.43.1.133 with SMTP id nq5mr27770477icb.21.1408560247682;
        Wed, 20 Aug 2014 11:44:07 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.140.92.102 with SMTP id a93ls428411qge.15.gmail; Wed, 20 Aug
 2014 11:44:06 -0700 (PDT)
X-Received: by 10.140.18.211 with SMTP id 77mr75541571qgf.57.1408560246854;
        Wed, 20 Aug 2014 11:44:06 -0700 (PDT)
Original-Received: from mail-qa0-x22b.google.com (mail-qa0-x22b.google.com [2607:f8b0:400d:c00::22b])
        by mx.google.com with ESMTPS id g106si35224091qge.32.2014.08.20.11.44.06
        for <std-proposals@isocpp.org>
        (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
        Wed, 20 Aug 2014 11:44:06 -0700 (PDT)
Received-SPF: pass (google.com: domain of gromer@google.com designates 2607:f8b0:400d:c00::22b as permitted sender) client-ip=2607:f8b0:400d:c00::22b;
Original-Received: by mail-qa0-f43.google.com with SMTP id w8so7357620qac.16
        for <std-proposals@isocpp.org>; Wed, 20 Aug 2014 11:44:06 -0700 (PDT)
X-Received: by 10.224.127.74 with SMTP id f10mr81742197qas.100.1408560246658;
 Wed, 20 Aug 2014 11:44:06 -0700 (PDT)
Original-Received: by 10.96.59.41 with HTTP; Wed, 20 Aug 2014 11:44:06 -0700 (PDT)
In-Reply-To: <75655d78-f533-40fe-8109-2725a5da77a9@isocpp.org>
X-Original-Sender: gromer@google.com
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of gromer@google.com designates 2607:f8b0:400d:c00::22b as permitted
 sender) smtp.mail=gromer@google.com;       dkim=pass header.i=@google.com;
       dmarc=pass (p=REJECT dis=NONE) header.from=google.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: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://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>,
 <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
X-Original-From: Geoffrey Romer <gromer@google.com>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:12384
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/12384>

--001a11c2cc1825d2f5050113fd68
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

On Wed, Aug 20, 2014 at 9:50 AM, Matthew Fioravante <fmatthew5876@gmail.com=
>
wrote:

>
> On Wednesday, August 20, 2014 11:47:21 AM UTC-4, David Rodr=C3=ADguez Ibe=
as
> wrote:
>>
>>
>> One possible way of tagging the code as intentional would be to
>> explicitly reset the unique pointer:
>>
>> auto x =3D make_unique<int>();
>> if (something) {
>>    v.push_back(std::move(x));
>>    x =3D 0;                                // explicitly say that the st=
ate
>> is known
>> }
>> foo(x.get());                            // don't warn
>>
>
> I like this approach but I'm not sure it can be made to be generic for al=
l
> movable types.
> Consider the case where the object is reused later.
>
> auto x =3D make_unique<int>();
> v.push_back(std::move(x));
> a(x.get()); //Warning: use after move
> x =3D make_unique<int>();
> b(x.get()); //compiler saw a new assignment to x, so we know this is vali=
d
> v.push_back(std::move(x));
> c(x.get()); //Warning: use after move
>
> x.reset(new int());
> d(x.get()); //???
>
> In the above example, the compiler needs to have know that
> unique_ptr::reset() is special and makes the object reusable again. Hard
> coding the rules for unique_ptr into a compiler warning may terrible
> idea idea since it is used so often but that approach cannot be generaliz=
ed.
>
>
> The diagnostic could be made to only trigger for const operations after
> move and be reset once a non-const operation occurs. This would not catch
> all of the kinds of use-after-move bugs, though. After moving, its safe t=
o
> do operations which overwrite the state but not operations which modify t=
he
> current state. We don't currently have any way to differentiate between t=
he
> 2.
>

How about an annotation on reset(), which indicates that it's the moral
equivalent of an assignment?

Alternatively, there's something to be said for standing by the convention
that you don't do anything to a moved-from value other than reassign or
destroy it, and therefore you _shouldn't_ use reset() in this kind of
situation.


>
>
> --
>
> ---
> 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.
> Visit this group at
> http://groups.google.com/a/isocpp.org/group/std-proposals/.
>

--=20

---=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.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposa=
ls/.

--001a11c2cc1825d2f5050113fd68
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div class=3D"gmail_extra"><br><div class=3D"gmail_quote">=
On Wed, Aug 20, 2014 at 9:50 AM, Matthew Fioravante <span dir=3D"ltr">&lt;<=
a href=3D"mailto:fmatthew5876@gmail.com" target=3D"_blank">fmatthew5876@gma=
il.com</a>&gt;</span> wrote:<br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div class=3D""><br>On Wedn=
esday, August 20, 2014 11:47:21 AM UTC-4, David Rodr=C3=ADguez Ibeas wrote:=
<blockquote style=3D"margin:0px 0px 0px 0.8ex;padding-left:1ex;border-left-=
color:rgb(204,204,204);border-left-width:1px;border-left-style:solid" class=
=3D"gmail_quote">
<div dir=3D"ltr"><br>One possible way of tagging the code as intentional wo=
uld be to explicitly reset the unique pointer:<br><br>auto x =3D make_uniqu=
e&lt;int&gt;();<br>if (something) {<br>=C2=A0 =C2=A0v.push_back(std::move(x=
));<br>=C2=A0 =C2=A0x =3D 0; =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// explic=
itly say that the state is known<br>

}<br>foo(x.get()); =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// don&#39;t warn<br></div></block=
quote><div>=C2=A0</div></div><div>I like this approach but=C2=A0I&#39;m not=
 sure it can be made=C2=A0to be generic for all movable types.</div><div>Co=
nsider the case where the object is reused later.</div>
<div class=3D""><div>=C2=A0</div><div>auto x =3D make_unique&lt;int&gt;();<=
/div></div><div>v.push_back(std::move(x));</div><div>a(x.get()); //Warning:=
 use after move</div><div class=3D""><div>x =3D make_unique&lt;int&gt;(); <=
/div></div>
<div>b(x.get()); //compiler saw a new assignment to x, so we know this is v=
alid</div><div>v.push_back(std::move(x));</div><div>c(x.get()); //Warning: =
use after move</div><div>=C2=A0</div><div>x.reset(new int()); </div><div>d(=
x.get()); //???</div>
<div>=C2=A0</div><div>In the above example, the compiler needs to have know=
 that unique_ptr::reset() is special and makes the object reusable again. H=
ard coding the rules for unique_ptr=C2=A0into a compiler=C2=A0warning may=
=C2=A0terrible idea=C2=A0idea since it is used so often but that approach c=
annot be generalized.</div>
<div>=C2=A0</div><div>=C2=A0</div><div>The diagnostic could be made to only=
 trigger for const operations after move and be reset once a non-const oper=
ation occurs. This would not catch all of the kinds of use-after-move bugs,=
 though. After moving, its safe to do operations which overwrite the state =
but not operations which modify the current state. We don&#39;t currently h=
ave any way to differentiate between the 2.</div>
</div></blockquote><div><br></div><div>How about an annotation on reset(), =
which indicates that it&#39;s the moral equivalent of an assignment?</div><=
div><br></div><div>Alternatively, there&#39;s something to be said for stan=
ding by the convention that you don&#39;t do anything to a moved-from value=
 other than reassign or destroy it, and therefore you _shouldn&#39;t_ use r=
eset() in this kind of situation.=C2=A0</div>
<div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8=
ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"><div>=C2=
=A0</div></div><div class=3D"HOEnZb"><div class=3D"h5">

<p></p>

-- <br>
<br>
--- <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" target=3D"_=
blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/" target=3D"_blank">http://groups.google.com/a/isocpp.org/gro=
up/std-proposals/</a>.<br>
</div></div></blockquote></div><br></div></div>

<p></p>

-- <br />
<br />
--- <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 />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

--001a11c2cc1825d2f5050113fd68--

.
