220 28048 <7857a4a6-29af-47c0-b6ab-c555a0005935@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Edward Catmur <ed@catmur.co.uk>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Specifier to cause the destructor to be called at
 last use instead of end of scope
Date: Tue, 30 Aug 2016 04:07:40 -0700 (PDT)
Lines: 273
Approved: news@gmane.org
Message-ID: <7857a4a6-29af-47c0-b6ab-c555a0005935@isocpp.org>
References: <ec2e95cc-3ef0-435b-aaf9-13e982c4583e@isocpp.org> <CAA7YVg3dJT3LY_L5Eg=ZNgfqqyOcX9P=JVv_pXTEx6xxsoGOdg@mail.gmail.com>
 <CAE7XuEWr7eyyjmqURnMwQf1pvyvHeMATM1MDjRY=25WuGy5Z=w@mail.gmail.com>
 <ce82b6f6-524e-4520-a3f3-aa3b1aa35940@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_337_73619561.1472555260123"
X-Trace: blaine.gmane.org 1472555271 24426 195.159.176.226 (30 Aug 2016 11:07:51 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Tue, 30 Aug 2016 11:07:51 +0000 (UTC)
Cc: nicolas.capens@gmail.com
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBDZLZTXF7UJBB7WRSW7AKGQEXAVKOTI@isocpp.org Tue Aug 30 13:07:46 2016
Return-path: <std-proposals+bncBDZLZTXF7UJBB7WRSW7AKGQEXAVKOTI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-yb0-f197.google.com ([209.85.213.197])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBDZLZTXF7UJBB7WRSW7AKGQEXAVKOTI@isocpp.org>)
	id 1begtL-0005fq-3q
	for gclcip-std-proposals@m.gmane.org; Tue, 30 Aug 2016 13:07:43 +0200
Original-Received: by mail-yb0-f197.google.com with SMTP id x93sf36862528ybh.2
        for <gclcip-std-proposals@m.gmane.org>; Tue, 30 Aug 2016 04:07:44 -0700 (PDT)
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=hCf+//VnlWPk9YCGRHqDi3R+bfqP+PsiBf4Xh/wchYM=;
        b=ccpKmvPHeMUtbSN/C1+AkG4h1dKoy6hwZqqQekhzB6q7pbsenQ1122kP+p9frFrxqN
         DjZzaoJGTfvlw1wo7vyDnfUzGcxBTQw3soXiYJB5eJQNBxRAJIG7D7pZOT/NOmrUep9c
         rbH7r0T7iQAttWPNilLkMmfPTIjiUQGxx8jQPO6EAqOEa6AF7D2NY06togylm2wimTj7
         VI4ey2CORBBRseuaaIMGdzI4T+/MWfa1gGG2kFvErFmurBY0PrHscgcGGA1kQwIMlNNS
         MykfjN9GTn6jEZwOqMO4NGW2YqoOgW8/A88gaSfOHXEBmwZ3xaSZV+ON/V+QzssnJk+V
         4noQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        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=hCf+//VnlWPk9YCGRHqDi3R+bfqP+PsiBf4Xh/wchYM=;
        b=J9I+WJ1xtI4mGsqGFUxiXItt3td7FAJuMHgmX1NwvVJ4vqegMkPe1NosbwOvWj0R7R
         o3M9Mzujx9PbisEP8mNfuG8/5pJi/mjH6QGty/ILS/heLo+V0fIKVC1Qcrv5YONRjWZe
         ZTigf+31dZIX6VmEctQ6IV+oiKllpbocArfk5cvYShq84TYul7J7rWSOyODp7flyecnj
         lwzNFZNZ6bYDfy2tgpcCf2vyqM/VLLh2+abT0h0fABCVBfbTWjPDfrGyjeHNuAdJHOQL
         2Jv/oZRbSl9N8EAnKbAf+IeqNuE/E2M+zHPLWXeG1QvdImecLehlNYv6Dj/uZJ7CoB5J
         4n5g==
X-Gm-Message-State: AE9vXwMT2W9MqH002mCIN+QBeuK+Ncfcu7VVkUTTlmqimL91p/EPsoEGpHlRRko35BACbg==
X-Received: by 10.200.37.20 with SMTP id 20mr2402982qtm.17.1472555263860;
        Tue, 30 Aug 2016 04:07:43 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.36.91.212 with SMTP id g203ls1052469itb.13.gmail; Tue, 30 Aug
 2016 04:07:42 -0700 (PDT)
X-Received: by 10.36.239.3 with SMTP id i3mr509968ith.2.1472555261304;
        Tue, 30 Aug 2016 04:07:41 -0700 (PDT)
In-Reply-To: <ce82b6f6-524e-4520-a3f3-aa3b1aa35940@isocpp.org>
X-Original-Sender: ed@catmur.co.uk
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:28048
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/28048>

------=_Part_337_73619561.1472555260123
Content-Type: multipart/alternative; 
	boundary="----=_Part_338_1043379957.1472555260124"

------=_Part_338_1043379957.1472555260124
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

On Tuesday, 30 August 2016 10:49:58 UTC+1, Andrzej Krzemie=C5=84ski wrote:
>
>
>
> W dniu czwartek, 25 sierpnia 2016 23:24:39 UTC+2 u=C5=BCytkownik Nicolas =
Capens=20
> napisa=C5=82:
>>
>> Thanks all for the initial feedback! It's clear to me now that I haven't=
=20
>> defined "last use" very well, and giving it an accurate definition based=
 on=20
>> my limited knowledge of compiler lingo is going to be tricky.
>>
>> So let me take a step back and describe what real-world issues I'm tryin=
g=20
>> to solve. The first example is one where I'd like the heap memory manage=
d=20
>> by an object to get freed at the same point where a scalar variable's=20
>> register would be made available for a different variable:
>>
>>     class Matrix
>>     {
>>     public:
>>         Matrix(size_t rows, size_t columns);
>>         auto ~Matrix();   // Eager destructor
>>         ...
>>     private:
>>         double *m;
>>     };
>>
>>     {
>>         Matrix a(10'000, 10'000) =3D ...;
>>         Matrix b(10'000, 10'000) =3D ...;
>>         Matrix c(10'000, 10'000) =3D ...;
>>
>>         a +=3D b;   // Last use of b, destruct before the next line
>>         a +=3D c;
>>      }
>>
>> The alternative of using explicit destruction is really inconvenient:
>>
>>     {
>>         Matrix a(10'000, 10'000) =3D ...;
>>         Matrix b(10'000, 10'000) =3D ...;
>>         Matrix c(10'000, 10'000) =3D ...;
>>
>>         a +=3D b;
>>         b.~Matrix();
>>         a +=3D c;
>>     }
>>
>> As is reducing the scope:
>>
>>     {
>>         Matrix a(10'000, 10'000) =3D ...;
>>
>>         {
>>             Matrix b(10'000, 10'000) =3D ...;
>>             a +=3D b;
>>         }
>>
>>         Matrix c(10'000, 10'000) =3D ...; =20
>>         a +=3D c;
>>     }
>>
>>
> Note that to some extent you can fix this problem with std::optional (in=
=20
> C++17, now in a TS):
>
> =20
> {
>   Matrix a(10'000, 10'000) =3D ...;
>   optional<Matrix> b(in_place, 10'000, 10'000) =3D ...;
>   Matrix c(10'000, 10'000) =3D ...;
>
>   a +=3D *b;
>   b =3D nullopt;
>   a +=3D c;
> }
>

(Reposting from correct account; sorry for the noise.)


You can do better than that using the exchange-empty idiom:

=20

a +=3D *std::exchange(b, nullopt);


However I believe that OP considers this also too clumsy (look for=20
discussion of std::move above).

=20

One thing I haven't seen mentioned yet in this discussion is that=20
allocation and deallocation is already elidable per [expr.new]/10. This=20
means that e.g. the destructor of std::string is not an observable=20
operation.

--=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/7857a4a6-29af-47c0-b6ab-c555a0005935%40isocpp.or=
g.

------=_Part_338_1043379957.1472555260124
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Tuesday, 30 August 2016 10:49:58 UTC+1, Andrzej Krzemie=
=C5=84ski  wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margi=
n-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"l=
tr"><br><br>W dniu czwartek, 25 sierpnia 2016 23:24:39 UTC+2 u=C5=BCytkowni=
k Nicolas Capens napisa=C5=82:<blockquote class=3D"gmail_quote" style=3D"ma=
rgin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex"><div =
dir=3D"ltr">Thanks all for the initial feedback! It&#39;s clear to me now t=
hat I haven&#39;t defined &quot;last use&quot; very well, and giving it an =
accurate definition based on my limited knowledge of compiler lingo is goin=
g to be tricky.<div><br></div><div>So let me take a step back and describe =
what real-world issues I&#39;m trying to solve. The first example is one wh=
ere I&#39;d like the heap memory managed by an object to get freed at the s=
ame point where a scalar variable&#39;s register would be made available fo=
r a different variable:</div><div><br></div><div><font face=3D"monospace">=
=C2=A0 =C2=A0 class Matrix</font></div><div><font face=3D"monospace">=C2=A0=
 =C2=A0 {</font></div><div><font face=3D"monospace">=C2=A0 =C2=A0 public:</=
font></div><div><font face=3D"monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 Matrix=
(size_t rows, size_t columns);</font></div><div><font face=3D"monospace">=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 auto ~Matrix(); =C2=A0 // Eager destructor</fon=
t></div><div><font face=3D"monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 ...</font=
></div><div><font face=3D"monospace">=C2=A0 =C2=A0 private:</font></div><di=
v><font face=3D"monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 double *m;</font></d=
iv><div><font face=3D"monospace">=C2=A0 =C2=A0 };</font></div><div><font fa=
ce=3D"monospace"><br></font></div><div><font face=3D"monospace">=C2=A0 =C2=
=A0 {</font></div><div><font face=3D"monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0=
 Matrix a(10&#39;000, 10&#39;000) =3D ...;</font></div><div><font face=3D"m=
onospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 Matrix b(10&#39;000, 10&#39;000) =3D =
....;<br></font></div><div><font face=3D"monospace">=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 Matrix c(10&#39;000, 10&#39;000) =3D ...;<br></font></div><div><font fa=
ce=3D"monospace"><br></font></div><div><font face=3D"monospace">=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 a=C2=A0+=3D b; =C2=A0 // Last use of b, destruct before t=
he next line</font></div><div><font face=3D"monospace">=C2=A0 =C2=A0 =C2=A0=
 =C2=A0 a=C2=A0+=3D c;</font></div><div><font face=3D"monospace">=C2=A0 =C2=
=A0 =C2=A0}</font></div><div><br></div><div dir=3D"ltr"><div>The alternativ=
e of using explicit destruction is really inconvenient:</div><div><br></div=
><div><div><font face=3D"monospace">=C2=A0 =C2=A0 {</font></div><div><font =
face=3D"monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 Matrix a(10&#39;000, 10&#39;=
000) =3D ...;</font></div><div><font face=3D"monospace">=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 Matrix b(10&#39;000, 10&#39;000) =3D ...;<br></font></div><div><=
font face=3D"monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 Matrix c(10&#39;000, 10=
&#39;000) =3D ...;<br></font></div><div><font face=3D"monospace"><br></font=
></div><div><font face=3D"monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 a=C2=A0+=
=3D b;</font></div><div><font face=3D"monospace">=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 b.~Matrix();</font></div><div><font face=3D"monospace">=C2=A0 =C2=A0 =
=C2=A0 =C2=A0 a=C2=A0+=3D c;</font></div><div><font face=3D"monospace">=C2=
=A0 =C2=A0 }</font></div></div><div><br></div><div>As is reducing the scope=
:</div><div><br></div><div><div><font face=3D"monospace">=C2=A0 =C2=A0 {</f=
ont></div><div><font face=3D"monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 Matrix =
a(10&#39;000, 10&#39;000) =3D ...;</font></div><div><font face=3D"monospace=
"><br></font></div><div><font face=3D"monospace">=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 {</font></div><div><font face=3D"monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0=
 =C2=A0 =C2=A0 Matrix b(10&#39;000, 10&#39;000) =3D ...;</font></div><div><=
font face=3D"monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 a=C2=A0+=
=3D b;</font></div><div><font face=3D"monospace">=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 }</font></div><div><font face=3D"monospace"><br>=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 Matrix c(10&#39;000, 10&#39;000) =3D ...;=C2=A0=C2=A0<br></font></di=
v><div><font face=3D"monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 a=C2=A0+=3D c;<=
/font></div><div><font face=3D"monospace">=C2=A0 =C2=A0 }</font></div></div=
><div><div><br></div></div></div></div></blockquote><div><br>Note that to s=
ome extent you can fix this problem with std::optional (in C++17, now in a =
TS):<br><br>=C2=A0<div style=3D"background-color:rgb(250,250,250);border-co=
lor:rgb(187,187,187);border-style:solid;border-width:1px;word-wrap:break-wo=
rd"><code><div><span style=3D"color:#660">{</span><span style=3D"color:#000=
"><br>=C2=A0 </span><span style=3D"color:#606">Matrix</span><span style=3D"=
color:#000"> a</span><span style=3D"color:#660">(</span><span style=3D"colo=
r:#066">10</span><span style=3D"color:#080">&#39;000, 10&#39;</span><span s=
tyle=3D"color:#066">000</span><span style=3D"color:#660">)</span><span styl=
e=3D"color:#000"> </span><span style=3D"color:#660">=3D</span><span style=
=3D"color:#000"> </span><span style=3D"color:#660">...;</span><span style=
=3D"color:#000"><br>=C2=A0 optional</span><span style=3D"color:#660">&lt;</=
span><span style=3D"color:#606">Matrix</span><span style=3D"color:#660">&gt=
;</span><span style=3D"color:#000"> b</span><span style=3D"color:#660">(</s=
pan><span style=3D"color:#000">in_place</span><span style=3D"color:#660">,<=
/span><span style=3D"color:#000"> </span><span style=3D"color:#066">10</spa=
n><span style=3D"color:#080">&#39;000, 10&#39;</span><span style=3D"color:#=
066">000</span><span style=3D"color:#660">)</span><span style=3D"color:#000=
"> </span><span style=3D"color:#660">=3D</span><span style=3D"color:#000"> =
</span><span style=3D"color:#660">...;</span><span style=3D"color:#000"><br=
>=C2=A0 </span><span style=3D"color:#606">Matrix</span><span style=3D"color=
:#000"> c</span><span style=3D"color:#660">(</span><span style=3D"color:#06=
6">10</span><span style=3D"color:#080">&#39;000, 10&#39;</span><span style=
=3D"color:#066">000</span><span style=3D"color:#660">)</span><span style=3D=
"color:#000"> </span><span style=3D"color:#660">=3D</span><span style=3D"co=
lor:#000"> </span><span style=3D"color:#660">...;</span><span style=3D"colo=
r:#000"><br><br>=C2=A0 a </span><span style=3D"color:#660">+=3D</span><span=
 style=3D"color:#000"> </span><span style=3D"color:#660">*</span><span styl=
e=3D"color:#000">b</span><span style=3D"color:#660">;</span><span style=3D"=
color:#000"><br>=C2=A0 b </span><span style=3D"color:#660">=3D</span><span =
style=3D"color:#000"> nullopt</span><span style=3D"color:#660">;</span><spa=
n style=3D"color:#000"><br>=C2=A0 a </span><span style=3D"color:#660">+=3D<=
/span><span style=3D"color:#000"> c</span><span style=3D"color:#660">;</spa=
n><span style=3D"color:#000"><br></span><span style=3D"color:#660">}</span>=
</div></code></div></div></div></blockquote><div><br></div><div><p class=3D=
"MsoNormal">(Reposting from correct account; sorry for the noise.)</p><p cl=
ass=3D"MsoNormal"><br></p><p class=3D"MsoNormal">You can do better than tha=
t using the exchange-empty idiom:<o:p></o:p></p>

<p class=3D"MsoNormal"><o:p>=C2=A0</o:p></p>

<p class=3D"MsoNormal" style=3D"background:#FAFAFA"></p><div class=3D"prett=
yprint" style=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-wor=
d; background-color: rgb(250, 250, 250);"><code class=3D"prettyprint"><div =
class=3D"subprettyprint"><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify">a </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
+=3D</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">*</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify">std</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify">exchange</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify">b</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"> nullopt</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">);</span></div></code></div><p></p><p class=3D"MsoNormal"><br></p>

<p class=3D"MsoNormal">However I believe that OP considers this also too cl=
umsy
(look for discussion of std::move above).<o:p></o:p></p>

<p class=3D"MsoNormal"><o:p>=C2=A0</o:p></p>

<p class=3D"MsoNormal">One thing I haven&#39;t seen mentioned yet in this d=
iscussion is
that allocation and deallocation is already elidable per [expr.new]/10. Thi=
s
means that e.g. the destructor of std::string is not an observable operatio=
n.<o:p></o:p></p></div><div><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/7857a4a6-29af-47c0-b6ab-c555a0005935%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/7857a4a6-29af-47c0-b6ab-c555a0005935=
%40isocpp.org</a>.<br />

------=_Part_338_1043379957.1472555260124--

------=_Part_337_73619561.1472555260123--

.
