Topic: Language support for scoped cleanup.
Author: ibaxter@gmail.com
Date: Tue, 13 May 2014 21:34:19 -0700 (PDT)
Raw View
------=_Part_212_19638708.1400042059950
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Is there any interest in language support for scoped cleanup?
RAII is well established as the =E2=80=98goto=E2=80=99 scoped management me=
thod, but it has=20
many limitations the greatest of which is the need to write wrapper classes=
=20
- which don=E2=80=99t have direct access to the local scope. There are a go=
od many=20
library=E2=80=99s that use RAII for scoped cleanup including std::unique_pt=
r, loki=20
scoped guard and boost scope exit. They all suffer from various problems,=
=20
such as heavy macro use and ugly syntax.
Shouldn't we take the hint and just add direct clean support in the=20
language itself?
I propose the use of a scope preceded by a ~ to mean scoped cleanup ( a=20
scoped destructor )=20
For example:
FILE* file=3D fopen(=E2=80=9Cfile.txy=E2=80=9D);
~{
fclose( file );
}
This can be thought of a creating a struct and instance similar to this:
struct __unknown__ {
~__unknown__ ( ) {
fclose( file );
}
} __unique_name__;
All variables from the enclosing scope are brought in similarly to a=20
reference capturing lambda [&]{ }.
Additionally a named scope could be use to =E2=80=98disarm=E2=80=99 the sco=
pe cleanup by=20
using assigning delete.
std::vector< int > container;
scope =3D ~{
container.pop_back( );
};
if ( some_condition ) {=20
container.push_back( 0 );
} else {
scope =3D delete;
}
this could be thought of as creating a struct like this:
struct __unknown__ {
__unknown__
: __fire__{ true }
{ }
~__unknown__ ( ) {
if ( __fire__ ) {
container.pop_back( );
}
}
void operator =3Ddelete( ) { // magic operator
__fire__ =3D false;
}
bool __fire__;
} __unique_name__;
A scoped destructor will be non-copyable or movable.
If there is interest in this I would gladly write up and submit a full=20
proposal.
Thoughts?
--=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/.
------=_Part_212_19638708.1400042059950
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><span id=3D"docs-internal-guid-79e99b30-f8fb-414e-495d-ad1=
8e2528c43"><p dir=3D"ltr" style=3D"line-height:1.15;margin-top:0pt;margin-b=
ottom:0pt;"><span style=3D"font-size: 15px; font-family: Arial; color: rgb(=
0, 0, 0); white-space: pre-wrap; background-color: transparent;">Is there a=
ny interest in language support for scoped cleanup?</span></p><br><p dir=3D=
"ltr" style=3D"line-height:1.15;margin-top:0pt;margin-bottom:0pt;"><span st=
yle=3D"font-size: 15px; font-family: Arial; color: rgb(0, 0, 0); white-spac=
e: pre-wrap; background-color: transparent;">RAII is well established as th=
e =E2=80=98goto=E2=80=99 scoped management method, but it has many limitati=
ons the greatest of which is the need to write wrapper classes - which don=
=E2=80=99t have direct access to the local scope. There are a good many lib=
rary=E2=80=99s that use RAII for scoped cleanup including std::unique_ptr, =
loki scoped guard and boost scope exit. They all suffer from various proble=
ms, such as heavy macro use and ugly syntax.</span></p><p dir=3D"ltr" style=
=3D"margin-top: 0pt; margin-bottom: 0pt;"><font color=3D"#000000" face=3D"A=
rial"><span style=3D"font-size: 15px; line-height: 17.25px; white-space: pr=
e-wrap;">Shouldn't</span></font><span style=3D"line-height: 1.15; font-size=
: 15px; font-family: Arial; color: rgb(0, 0, 0); white-space: pre-wrap; bac=
kground-color: transparent;"> we take the hint and just add direct clean su=
pport in the language itself?</span></p><br><p dir=3D"ltr" style=3D"line-he=
ight:1.15;margin-top:0pt;margin-bottom:0pt;"><span style=3D"font-size: 15px=
; font-family: Arial; color: rgb(0, 0, 0); white-space: pre-wrap; backgroun=
d-color: transparent;">I propose the use of a scope preceded by a ~ to mean=
scoped cleanup ( a scoped destructor ) </span></p><br><p dir=3D"ltr" style=
=3D"line-height:1.15;margin-top:0pt;margin-bottom:0pt;"><span style=3D"font=
-size: 15px; font-family: Arial; color: rgb(0, 0, 0); white-space: pre-wrap=
; background-color: transparent;">For example:</span></p><p dir=3D"ltr" sty=
le=3D"line-height:1.15;margin-top:0pt;margin-bottom:0pt;"><span style=3D"fo=
nt-size: 15px; font-family: Arial; color: rgb(0, 0, 0); white-space: pre-wr=
ap; background-color: transparent;"><br></span></p><div class=3D"prettyprin=
t" style=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-word; ba=
ckground-color: rgb(250, 250, 250);"><code class=3D"prettyprint"><div class=
=3D"subprettyprint"><span style=3D"color: #000;" class=3D"styled-by-prettif=
y">FILE</span><span style=3D"color: #660;" class=3D"styled-by-prettify">*</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"> file</span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> fopen</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">(=E2=80=9C</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">file</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">.</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify">txy</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">=E2=80=9D);</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">~{</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"><br> fclose</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> file </span><span style=3D"color: #660;" class=3D"styled-=
by-prettify">);</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify"><br></span><span style=3D"color: #660;" class=3D"styled-by-prettify">}=
</span></div></code></div><p dir=3D"ltr" style=3D"line-height:1.15;margin-t=
op:0pt;margin-bottom:0pt;"><span style=3D"color: rgb(0, 0, 0); white-space:=
pre-wrap; background-color: transparent;"><font face=3D"courier new, monos=
pace" size=3D"2"><br></font></span></p><div><br></div><p dir=3D"ltr" style=
=3D"line-height:1.15;margin-top:0pt;margin-bottom:0pt;"><span style=3D"font=
-size: 15px; font-family: Arial; color: rgb(0, 0, 0); white-space: pre-wrap=
; background-color: transparent;">This can be thought of a creating a struc=
t and instance similar to this:</span></p><font face=3D"courier new, monosp=
ace"><div><br></div><br></font><div class=3D"prettyprint" style=3D"border: =
1px solid rgb(187, 187, 187); word-wrap: break-word; background-color: rgb(=
250, 250, 250);"><code class=3D"prettyprint"><div class=3D"subprettyprint">=
<span style=3D"color: #008;" class=3D"styled-by-prettify">struct</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify"> __unknown__ </span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"><br> </span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">~</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">__unknown__ </span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;=
" class=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"><br> fclose</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> file </span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">);</span><span style=3D"color: #000;" class=3D"styled-=
by-prettify"><br> </span><span style=3D"color: #660;" class=3D=
"styled-by-prettify">}</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify"><br></span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">}</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> __=
unique_name__</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">;</span></div></code></div><p dir=3D"ltr" style=3D"line-height:1.15;marg=
in-top:0pt;margin-bottom:0pt;"><span style=3D"font-size: 15px; color: rgb(0=
, 0, 0); white-space: pre-wrap; background-color: transparent;"><font face=
=3D"courier new, monospace"><br></font></span></p><br><br><p dir=3D"ltr" st=
yle=3D"margin-top: 0pt; margin-bottom: 0pt;"><span style=3D"background-colo=
r: transparent;"><font color=3D"#000000" face=3D"Arial"><span style=3D"font=
-size: 15px; line-height: 1.15; white-space: pre-wrap;">All variables from =
the enclosing scope are brought in </span><span style=3D"font-size: 15px; l=
ine-height: 17.25px; white-space: pre-wrap;">similarly</span><span style=3D=
"font-size: 15px; line-height: 1.15; white-space: pre-wrap;"> to a referenc=
e capturing lambda [&]{ }.</span></font></span></p><br><br><p dir=3D"lt=
r" style=3D"line-height:1.15;margin-top:0pt;margin-bottom:0pt;"><span style=
=3D"font-size: 15px; font-family: Arial; color: rgb(0, 0, 0); white-space: =
pre-wrap; background-color: transparent;">Additionally a named scope could =
be use to =E2=80=98disarm=E2=80=99 the scope cleanup by using assigni=
ng delete.</span></p><div><span><br></span></div><div><span><br></span></di=
v><div class=3D"prettyprint" style=3D"border: 1px solid rgb(187, 187, 187);=
word-wrap: break-word; background-color: rgb(250, 250, 250);"><code class=
=3D"prettyprint"><div class=3D"subprettyprint"><span style=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"styl=
ed-by-prettify">vector</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify"><</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">int=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">></span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"> container</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"><br>scope </span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">=3D</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">~{</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"><br> container</span><span style=3D"color: #6=
60;" class=3D"styled-by-prettify">.</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify">pop_back</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">);</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><b=
r></span><span style=3D"color: #660;" class=3D"styled-by-prettify">};</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br></span><=
span style=3D"color: #008;" class=3D"styled-by-prettify">if</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"> some_condition </span><span style=3D"color:=
#660;" class=3D"styled-by-prettify">)</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify"> <br> container</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">.</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify">push_back</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> </span><span style=3D"color: #066;" class=3D"styled-by-prettify">=
0</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">);</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">}</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" clas=
s=3D"styled-by-prettify">else</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
><br> scope </span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">d=
elete</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">}</span></div></code=
></div><p dir=3D"ltr" style=3D"line-height:1.15;margin-top:0pt;margin-botto=
m:0pt;"><span style=3D"font-size: 15px; color: rgb(0, 0, 0); white-space: p=
re-wrap; background-color: transparent;"><font face=3D"courier new, monospa=
ce"><br></font></span></p><br><p dir=3D"ltr" style=3D"line-height:1.15;marg=
in-top:0pt;margin-bottom:0pt;"><span style=3D"font-size: 15px; font-family:=
Arial; color: rgb(0, 0, 0); white-space: pre-wrap; background-color: trans=
parent;">this could be thought of as creating a struct like this:</span></p=
><div><span><br></span></div><div><br></div><div class=3D"prettyprint" styl=
e=3D"border: 1px solid rgb(187, 187, 187); word-wrap: break-word; backgroun=
d-color: rgb(250, 250, 250);"><code class=3D"prettyprint"><div class=3D"sub=
prettyprint"><span style=3D"color: #008;" class=3D"styled-by-prettify">stru=
ct</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> __unkno=
wn__ </span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"><br> &nb=
sp; __unknown__<br> </span><span style=3D"color:=
#660;" class=3D"styled-by-prettify">:</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify"> __fire__</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-p=
rettify">true</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">}</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"><br> &nbs=
p; </span><span style=3D"color: #660;" class=3D"styled-by-prettify">{</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">}</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"><br><br> </span><span=
style=3D"color: #660;" class=3D"styled-by-prettify">~</span><span style=3D=
"color: #000;" class=3D"styled-by-prettify">__unknown__ </span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"><br> </span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">if</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> _=
_fire__ </span><span style=3D"color: #660;" class=3D"styled-by-prettify">)<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br> &nb=
sp; container</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">.</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify">pop_back</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">);</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"><br> &n=
bsp; </span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">}</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>&nb=
sp; </span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>}</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br>=
</span><span style=3D"color: #008;" class=3D"styled-by-pretti=
fy">void</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <=
/span><span style=3D"color: #008;" class=3D"styled-by-prettify">operator</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span=
style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">delete</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">)</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> &=
nbsp;</span><span style=3D"color: #800;" class=3D"styled-by-prettify">// ma=
gic operator</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"><br> __fire__ </span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"sty=
led-by-prettify">false</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"><br> </span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">}</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
"><br><br> </span><span style=3D"color: #008;" class=3D"styled=
-by-prettify">bool</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> __fire__</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><b=
r><br></span><span style=3D"color: #660;" class=3D"styled-by-prettify">}</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> __unique_nam=
e__</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br></span><=
/div></code></div><div><br><span><br></span></div><div><br></div><p dir=3D"=
ltr" style=3D"margin-top: 0pt; margin-bottom: 0pt;"><span style=3D"backgrou=
nd-color: transparent;"><font color=3D"#000000" face=3D"Arial"><span style=
=3D"font-size: 15px; line-height: 1.15; white-space: pre-wrap;">A scoped de=
structor will be non-</span><span style=3D"font-size: 15px; line-height: 17=
..25px; white-space: pre-wrap;">copyable</span><span style=3D"font-size: 15p=
x; line-height: 1.15; white-space: pre-wrap;"> or movable.</span></font></s=
pan></p><br><p dir=3D"ltr" style=3D"line-height:1.15;margin-top:0pt;margin-=
bottom:0pt;"><span style=3D"font-size: 15px; font-family: Arial; color: rgb=
(0, 0, 0); white-space: pre-wrap; background-color: transparent;">If there =
is interest in this I would gladly write up and submit a full proposal.</sp=
an></p><br><p dir=3D"ltr" style=3D"line-height:1.15;margin-top:0pt;margin-b=
ottom:0pt;"><span style=3D"font-size: 15px; font-family: Arial; color: rgb(=
0, 0, 0); white-space: pre-wrap; background-color: transparent;">Thoughts?<=
/span></p><div><span style=3D"font-size: 15px; font-family: Arial; color: r=
gb(0, 0, 0); white-space: pre-wrap; background-color: transparent;"><br></s=
pan></div></span></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" 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 />
------=_Part_212_19638708.1400042059950--
.
Author: Peter Sommerlad <psommerl@hsr.ch>
Date: Wed, 14 May 2014 09:32:15 +0200
Raw View
Hi Ira,
the general rule of the C++ standards committee is to do something with a l=
ibrary feature, if it can be done with it and
only extend the core language, if a library feature alone isn't able to sol=
ve it.
There is a whole in the current library that you found, but with an extensi=
on I proposed and C++11 lambdas you get quite close
to what you want without any special language feature.
see http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3949.pdf
(There might be a slight update on it for the Rapperswil Meeting).
(Ira, if you want to discuss it in detail, we can do so next week I guess a=
nd may be improve my paper.)
Regards
Peter.
On 14.05.2014, at 06:34, <ibaxter@gmail.com> <ibaxter@gmail.com> wrote:
> Is there any interest in language support for scoped cleanup?
>=20
> RAII is well established as the 'goto' scoped management method, but it h=
as many limitations the greatest of which is the need to write wrapper clas=
ses - which don't have direct access to the local scope. There are a good m=
any library's that use RAII for scoped cleanup including std::unique_ptr, l=
oki scoped guard and boost scope exit. They all suffer from various problem=
s, such as heavy macro use and ugly syntax.
> Shouldn't we take the hint and just add direct clean support in the langu=
age itself?
>=20
> I propose the use of a scope preceded by a ~ to mean scoped cleanup ( a s=
coped destructor )=20
>=20
> For example:
>=20
> FILE* file=3D fopen("file.txy");
> ~{
> fclose( file );
> }
>=20
>=20
> This can be thought of a creating a struct and instance similar to this:
>=20
>=20
> struct __unknown__ {
> ~__unknown__ ( ) {
> fclose( file );
> }
> } __unique_name__;
>=20
>=20
>=20
> All variables from the enclosing scope are brought in similarly to a refe=
rence capturing lambda [&]{ }.
>=20
>=20
> Additionally a named scope could be use to 'disarm' the scope cleanup by =
using assigning delete.
>=20
>=20
> std::vector< int > container;
> scope =3D ~{
> container.pop_back( );
> };
>=20
> if ( some_condition ) {=20
> container.push_back( 0 );
> } else {
> scope =3D delete;
> }
>=20
>=20
> this could be thought of as creating a struct like this:
>=20
>=20
> struct __unknown__ {
> __unknown__
> : __fire__{ true }
> { }
>=20
> ~__unknown__ ( ) {
> if ( __fire__ ) {
> container.pop_back( );
> }
> }
>=20
> void operator =3Ddelete( ) { // magic operator
> __fire__ =3D false;
> }
>=20
> bool __fire__;
>=20
> } __unique_name__;
>=20
>=20
>=20
>=20
> A scoped destructor will be non-copyable or movable.
>=20
> If there is interest in this I would gladly write up and submit a full pr=
oposal.
>=20
> Thoughts?
>=20
>=20
> --=20
>=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=
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-propo=
sals/.
--=20
Prof. Peter Sommerlad
Institut f=FCr Software: Bessere Software - Einfach, Schneller!
HSR Hochschule f=FCr Technik Rapperswil
Oberseestr 10, Postfach 1475, CH-8640 Rapperswil
http://ifs.hsr.ch http://cute-test.com http://linticator.com http://includa=
tor.com
tel:+41 55 222 49 84 =3D=3D mobile:+41 79 432 23 32
fax:+41 55 222 46 29 =3D=3D mailto:peter.sommerlad@hsr.ch
--=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/.
.
Author: ibaxter@gmail.com
Date: Wed, 14 May 2014 17:29:37 -0700 (PDT)
Raw View
------=_Part_7421_26533249.1400113777859
Content-Type: text/plain; charset=UTF-8
Simple things should be simple. C++ prides itself on having deterministic
resource clean-up, but unfortunately it's often not simple. With language
support exception safe clean up can be accomplished with just 3 characters
extra ~{}, syntax matters.
A library solution can only get you so far, not only do you have to contend
with overly complex syntax, but performance issues as well. scope_guard
will almost certainly be implemented by std::function holding the lambda
and that means potentially an allocation, a deallocation and a throw. Given
the conception logic flow simplification that scoped management enables,
this idiom should be encouraged. An allocation is a non-starter. Scoped
clean-up should be zero cost.
Discussion on what the std committee will or will not accept is premature,
this is just an idea at this stage, one that I think has merit. The c++
committee has stated language features will be considered for c++17, well
OK, here is one.
Its a pure extension that does not affect any existing code, and introduces
no new keywords. It vastly simplifies exception safe clean-up, a common
task that should be supported in the language IMHO.
I think it is worth having a discussion on what ideally scoped management
should look like. What is the simplest, cleanest practical design?
Evangelizing it for committee consideration can come later.
It is interesting to note just how many c++11 language features did in fact
have already existing -somewhat adequate- library implementations.
Cheers
Ian
--
---
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/.
------=_Part_7421_26533249.1400113777859
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div>Simple things should be simple. C++ prides itself on =
having deterministic resource clean-up, but unfortunately it's often not si=
mple. With language support exception safe clean up can be accomplished wit=
h just 3 characters extra ~{}, syntax matters.</div><div><br></div><div>A l=
ibrary solution can only get you so far, not only do you have to contend wi=
th overly complex syntax, but performance issues as well. scope_guard will =
almost certainly be implemented by std::function holding the lambda and tha=
t means potentially an allocation, a deallocation and a throw. Given the co=
nception logic flow simplification that scoped management enables, this idi=
om should be encouraged. An allocation is a non-starter. Scoped clean-up sh=
ould be zero cost.</div><div><br></div><div>Discussion on what the std comm=
ittee will or will not accept is premature, this is just an idea at this st=
age, one that I think has merit. The c++ committee has stated language feat=
ures will be considered for c++17, well OK, here is one. </div><div>It=
s a pure extension that does not affect any existing code, and introduces n=
o new keywords. It vastly simplifies exception safe clean-up, a common task=
that should be supported in the language IMHO. </div><div><br></div><=
div>I think it is worth having a discussion on what ideally scoped manageme=
nt should look like. What is the simplest, cleanest practical design? Evang=
elizing it for committee consideration can come later. </div><div>It i=
s interesting to note just how many c++11 language features did in fact hav=
e already existing -somewhat adequate- library implementations.</div><div><=
br></div><div>Cheers</div><div><br></div><div>Ian</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" 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 />
------=_Part_7421_26533249.1400113777859--
.
Author: Andrew Sandoval <sandoval@netwaysglobal.com>
Date: Wed, 14 May 2014 19:25:24 -0700 (PDT)
Raw View
------=_Part_608_23154603.1400120724988
Content-Type: text/plain; charset=UTF-8
On Wednesday, May 14, 2014 7:29:37 PM UTC-5, iba...@gmail.com wrote:
>
> Simple things should be simple. C++ prides itself on having deterministic
> resource clean-up, but unfortunately it's often not simple. With language
> support exception safe clean up can be accomplished with just 3 characters
> extra ~{}, syntax matters.
>
> A library solution can only get you so far, not only do you have to
> contend with overly complex syntax, but performance issues as well.
> scope_guard will almost certainly be implemented by std::function holding
> the lambda and that means potentially an allocation, a deallocation and a
> throw. Given the conception logic flow simplification that scoped
> management enables, this idiom should be encouraged. An allocation is a
> non-starter. Scoped clean-up should be zero cost.
>
> Discussion on what the std committee will or will not accept is premature,
> this is just an idea at this stage, one that I think has merit. The c++
> committee has stated language features will be considered for c++17, well
> OK, here is one.
> Its a pure extension that does not affect any existing code, and
> introduces no new keywords. It vastly simplifies exception safe clean-up, a
> common task that should be supported in the language IMHO.
>
> I think it is worth having a discussion on what ideally scoped management
> should look like. What is the simplest, cleanest practical design?
> Evangelizing it for committee consideration can come later.
> It is interesting to note just how many c++11 language features did in
> fact have already existing -somewhat adequate- library implementations.
>
> Cheers
>
> Ian
>
Ian,
The classes in proposal N3949 were designed to avoid issues with allocation
failure. They do NOT use std::function. Please read through
the proposal. I for one would be happy to have someone take the time to do
a second proposal that covers shared resources (as I think it should remain
separate). Your idea is not without merit, but you should give careful
consideration to what Peter has said. He knows what he is talking about.
You may also wish to read through the previous proposals that are linked to
N3949 to see how this has progressed with committee feedback.
Best Wishes,
Andrew Sandoval
--
---
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/.
------=_Part_608_23154603.1400120724988
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">On Wednesday, May 14, 2014 7:29:37 PM UTC-5, iba...@gmail.=
com wrote:<blockquote class=3D"gmail_quote" 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;"><div dir=3D"ltr"><div>Simple things =
should be simple. C++ prides itself on having deterministic resource clean-=
up, but unfortunately it's often not simple. With language support exceptio=
n safe clean up can be accomplished with just 3 characters extra ~{}, synta=
x matters.</div><div><br></div><div>A library solution can only get you so =
far, not only do you have to contend with overly complex syntax, but perfor=
mance issues as well. scope_guard will almost certainly be implemented by s=
td::function holding the lambda and that means potentially an allocation, a=
deallocation and a throw. Given the conception logic flow simplification t=
hat scoped management enables, this idiom should be encouraged. An allocati=
on is a non-starter. Scoped clean-up should be zero cost.</div><div><br></d=
iv><div>Discussion on what the std committee will or will not accept is pre=
mature, this is just an idea at this stage, one that I think has merit. The=
c++ committee has stated language features will be considered for c++17, w=
ell OK, here is one. </div><div>Its a pure extension that does not aff=
ect any existing code, and introduces no new keywords. It vastly simplifies=
exception safe clean-up, a common task that should be supported in the lan=
guage IMHO. </div><div><br></div><div>I think it is worth having a dis=
cussion on what ideally scoped management should look like. What is the sim=
plest, cleanest practical design? Evangelizing it for committee considerati=
on can come later. </div><div>It is interesting to note just how many =
c++11 language features did in fact have already existing -somewhat adequat=
e- library implementations.</div><div><br></div><div>Cheers</div><div><br><=
/div><div>Ian</div></div></blockquote><div><br></div><div>Ian,</div><div><b=
r></div><div>The classes in proposal N3949 were designed to avoid =
;issues with allocation failure. They do NOT use std::function.&=
nbsp; Please read through the proposal. I for one would be =
happy to have someone take the time to do a second proposal that covers sha=
red resources (as I think it should remain separate). Your idea is no=
t without merit, but you should give careful consideration to what Pet=
er has said. He knows what he is talking about. You may also wi=
sh to read through the previous proposals that are linked to N3949 to see h=
ow this has progressed with committee feedback.</div><div><br></div><div>Be=
st Wishes,</div><div>Andrew Sandoval </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" 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 />
------=_Part_608_23154603.1400120724988--
.
Author: vadim.petrochenkov@gmail.com
Date: Thu, 15 May 2014 03:50:36 -0700 (PDT)
Raw View
------=_Part_3273_15342784.1400151036409
Content-Type: text/plain; charset=UTF-8
>>Simple things should be simple
Is auto&& fin = std::make_finally([&] { cleanup(x, y, z); }); short and
simple enough?
(Or even std::finally fin([&] { cleanup(x, y, z); }); if N3602 is
completed.)
If yes, then it can be done with library without special new syntax (and
without std::function). And it is already done in the form of mentioned
scope_guard (though I think scope_guard is still a bit less simple than it
should be.)
You may also be interested in this discussion:
https://groups.google.com/a/isocpp.org/forum/?fromgroups#!searchin/std-proposals/finally/std-proposals/Om0fMEJnvcM/U-AGMVfJUiMJ
On Thursday, May 15, 2014 4:29:37 AM UTC+4, iba...@gmail.com wrote:
>
> Simple things should be simple. C++ prides itself on having deterministic
> resource clean-up, but unfortunately it's often not simple. With language
> support exception safe clean up can be accomplished with just 3 characters
> extra ~{}, syntax matters.
>
> A library solution can only get you so far, not only do you have to
> contend with overly complex syntax, but performance issues as well.
> scope_guard will almost certainly be implemented by std::function holding
> the lambda and that means potentially an allocation, a deallocation and a
> throw. Given the conception logic flow simplification that scoped
> management enables, this idiom should be encouraged. An allocation is a
> non-starter. Scoped clean-up should be zero cost.
>
> Discussion on what the std committee will or will not accept is premature,
> this is just an idea at this stage, one that I think has merit. The c++
> committee has stated language features will be considered for c++17, well
> OK, here is one.
> Its a pure extension that does not affect any existing code, and
> introduces no new keywords. It vastly simplifies exception safe clean-up, a
> common task that should be supported in the language IMHO.
>
> I think it is worth having a discussion on what ideally scoped management
> should look like. What is the simplest, cleanest practical design?
> Evangelizing it for committee consideration can come later.
> It is interesting to note just how many c++11 language features did in
> fact have already existing -somewhat adequate- library implementations.
>
> Cheers
>
> Ian
>
--
---
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/.
------=_Part_3273_15342784.1400151036409
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><p>>>Simple things should be simple</p><p>Is <span s=
tyle=3D"color: #008;" class=3D"styled-by-prettify">auto</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">&&</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> fin </span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">=3D</span><span style=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">make_finally</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">([&]</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">{</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"> cleanup</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">x</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span =
style=3D"color: #000;" class=3D"styled-by-prettify"> y</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"> z</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">);</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">});</span> short and simple enough?</p><p>(Or even <span style=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: #008;" =
class=3D"styled-by-prettify">finally</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> fin</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">([&]</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">{</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
cleanup</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify">x</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> y</span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">,</span><span style=3D"color: #000;"=
class=3D"styled-by-prettify"> z</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">);</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">}); </span>if N3602 is completed.)</p><p>If yes, then it can be done=
with library without special new syntax (and without std::function). And i=
t is already done in the form of mentioned scope_guard (though I think scop=
e_guard is still a bit less simple than it should be.) </p><p>You may =
also be interested in this discussion: https://groups.google.com/a/isocpp.o=
rg/forum/?fromgroups#!searchin/std-proposals/finally/std-proposals/Om0fMEJn=
vcM/U-AGMVfJUiMJ<br></p>On Thursday, May 15, 2014 4:29:37 AM UTC+4, iba...@=
gmail.com 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"lt=
r"><div>Simple things should be simple. C++ prides itself on having determi=
nistic resource clean-up, but unfortunately it's often not simple. With lan=
guage support exception safe clean up can be accomplished with just 3 chara=
cters extra ~{}, syntax matters.</div><div><br></div><div>A library solutio=
n can only get you so far, not only do you have to contend with overly comp=
lex syntax, but performance issues as well. scope_guard will almost certain=
ly be implemented by std::function holding the lambda and that means potent=
ially an allocation, a deallocation and a throw. Given the conception logic=
flow simplification that scoped management enables, this idiom should be e=
ncouraged. An allocation is a non-starter. Scoped clean-up should be zero c=
ost.</div><div><br></div><div>Discussion on what the std committee will or =
will not accept is premature, this is just an idea at this stage, one that =
I think has merit. The c++ committee has stated language features will be c=
onsidered for c++17, well OK, here is one. </div><div>Its a pure exten=
sion that does not affect any existing code, and introduces no new keywords=
.. It vastly simplifies exception safe clean-up, a common task that should b=
e supported in the language IMHO. </div><div><br></div><div>I think it=
is worth having a discussion on what ideally scoped management should look=
like. What is the simplest, cleanest practical design? Evangelizing it for=
committee consideration can come later. </div><div>It is interesting =
to note just how many c++11 language features did in fact have already exis=
ting -somewhat adequate- library implementations.</div><div><br></div><div>=
Cheers</div><div><br></div><div>Ian</div></div></blockquote></div>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" 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 />
------=_Part_3273_15342784.1400151036409--
.