Topic: extending the use of assignment as a condition expression?


Author: Evan Teran <evan.teran@gmail.com>
Date: Sun, 26 Jan 2014 11:42:50 -0800 (PST)
Raw View
------=_Part_155_415361.1390765370748
Content-Type: text/plain; charset=UTF-8


I love that we can write things like this:

if(auto p = dynamic_cast<T *>(x)) {
    // use p here
}

it is impossible to use p when the cast fails because it doesn't exist
outside the scope of the if. One thing that I've wished we could do was to
do the same, but with constructor syntax. So for example, I'd like to be
able to do this:


if(T x(y)) {
    // use x here
}

In my mind, this would be allowed IFF the type T has an explicit operator
bool() implemented. Additionally, as one might expect, the destructor would
be called at the end of the if's scope. The primary reason I'd like to see
this would be to improve the ease of using RAII for things like mutex's,
file's, etc. The typical approach is to move such code into a function, and
while that works well, sometimes it's hard to justify the boilerplate for a
small snippet which needs a gaurd.

Here's are some examples that I can imagine:


if(std::unique_lock<std::mutex>(m)) {
    // we successfully acquired the mutex, it will be automatically unlocked
    // when we exit this scope
}

//the mutex is unlocked at this point, do some non-thread sentive work

if(std::unique_lock<std::mutex>(m)) {
    // do other work inside the mutex
}

similarly, we could the the same with files:

if(std::ifstream file("some_file.txt")) {
    // we successfully opened the file!
}


In all fairness, we might be able to "fake it" now that we have the ability
to move with something like this:

if(std::unique_lock<std::mutex> l = std::move(std::unique_lock<std::mutex>(m
))) {
    // i think this would work... but ewww
}


I think this would probably work, but it's ugly. Simply adding the ability
for the constructor syntax to be allowed when an explicit operator bool is
present makes for some very nice and simply syntax. And personally, I think
it reads a little easier too :-)

Thoughts/Comments/Criticisms?
Evan

--

---
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_155_415361.1390765370748
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><div>I love that we can write things like this:</div><=
div><br></div><div class=3D"prettyprint" style=3D"background-color: rgb(250=
, 250, 250); border: 1px solid rgb(187, 187, 187); word-wrap: break-word;">=
<code class=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"co=
lor: #008;" class=3D"styled-by-prettify">if</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">(</span><span style=3D"color: #008;" class=
=3D"styled-by-prettify">auto</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> p </span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> </span><span style=3D"color: #008;" class=3D"styled-by-prettify">dynam=
ic_cast</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&lt=
;</span><span style=3D"color: #000;" class=3D"styled-by-prettify">T </span>=
<span style=3D"color: #660;" class=3D"styled-by-prettify">*&gt;(</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify">x</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">))</span><span style=3D"colo=
r: #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>&nbsp; &nbsp; </span><span style=3D"color: #800;" =
class=3D"styled-by-prettify">// use p here</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">}</span></div></code></div><div><br></div><div>it=
 is impossible to use p when the cast fails because it doesn't exist outsid=
e the scope of the if. One thing that I've wished we could do was to do the=
 same, but with constructor syntax. So for example, I'd like to be able to =
do this:</div><div><br></div><div><br></div><div class=3D"prettyprint" styl=
e=3D"background-color: rgb(250, 250, 250); border: 1px solid rgb(187, 187, =
187); word-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"sub=
prettyprint"><span style=3D"color: #008;" class=3D"styled-by-prettify">if</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify">T x</span><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"> </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>&nbsp; &nbsp; </span><span style=3D"color: #800;" class=3D"styled-by=
-prettify">// use x here</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify"><br></span><span style=3D"color: #660;" class=3D"styled-by-pr=
ettify">}</span></div></code></div><div><br></div><div>In my mind, this wou=
ld be allowed IFF the type T has an explicit operator bool() implemented. A=
dditionally, as one might expect, the destructor would be called at the end=
 of the if's scope. The primary reason I'd like to see this would be to imp=
rove the ease of using RAII for things like mutex's, file's, etc. The typic=
al approach is to move such code into a function, and while that works well=
, sometimes it's hard to justify the boilerplate for a small snippet which =
needs a gaurd.</div><div><br></div><div>Here's are some examples that I can=
 imagine:</div><div><br></div><div class=3D"prettyprint" style=3D"backgroun=
d-color: rgb(250, 250, 250); border: 1px solid rgb(187, 187, 187); word-wra=
p: break-word;"><code class=3D"prettyprint"><div class=3D"subprettyprint"><=
span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span s=
tyle=3D"color: #008;" class=3D"styled-by-prettify">if</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify">std</span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify">unique_lock</span><span style=3D"color: #660;" class=3D"=
styled-by-prettify">&lt;</span><font color=3D"#000000"><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">mutex</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">&gt;</span></font><span style=3D"color: #660;" class=3D"=
styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by=
-prettify">m</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">))</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"><br>&nbsp; &nbsp; </span=
><span style=3D"color: #800;" class=3D"styled-by-prettify">// we successful=
ly acquired the mutex, it will be automatically unlocked</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br>&nbsp; &nbsp; </span><sp=
an style=3D"color: #800;" class=3D"styled-by-prettify">// when we exit this=
 scope</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">}</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"><br><br></span><spa=
n style=3D"color: #800;" class=3D"styled-by-prettify">//the mutex is unlock=
ed at this point, do some non-thread sentive work</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"><br><br></span><span style=3D"color:=
 #008;" class=3D"styled-by-prettify">if</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">(</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-prett=
ify">unique_lock</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">&lt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify">mutex</span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">&gt;(</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify">m</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">))</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br>&nbsp; &nbsp; </span><span style=3D"color: #800;" class=
=3D"styled-by-prettify">// do other work inside the mutex</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">}</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"><br></span></div></code></div><div><br></di=
v><div>similarly, we could the the same with files:</div><div><br></div><di=
v class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); bord=
er: 1px solid rgb(187, 187, 187); word-wrap: break-word;"><code class=3D"pr=
ettyprint"><div class=3D"subprettyprint"><span style=3D"color: #008;" class=
=3D"styled-by-prettify">if</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify">std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
::</span><span style=3D"color: #000;" class=3D"styled-by-prettify">ifstream=
 file</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</sp=
an><span style=3D"color: #080;" class=3D"styled-by-prettify">"some_file.txt=
"</span><span style=3D"color: #660;" class=3D"styled-by-prettify">))</span>=
<span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"><br>&nbsp; &nbsp; </span><span styl=
e=3D"color: #800;" class=3D"styled-by-prettify">// we successfully opened t=
he file!</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>=
</div></code></div><div><br></div><div><br></div><div>In all fairness, we m=
ight be able to "fake it" now that we have the ability to move with somethi=
ng like this:</div><br><div class=3D"prettyprint" style=3D"background-color=
: rgb(250, 250, 250); border: 1px solid rgb(187, 187, 187); word-wrap: brea=
k-word;"><code class=3D"prettyprint"><div class=3D"subprettyprint"><span st=
yle=3D"color: #008;" class=3D"styled-by-prettify">if</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify">std</span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify">unique_lock</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">&lt;</span><span style=3D"color: #000;" class=3D"styled-=
by-prettify">std</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">::</span><span style=3D"color: #000;" class=3D"styled-by-prettify">mu=
tex</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&gt;</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> l </span><sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> std</span><span style=3D"c=
olor: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #0=
00;" class=3D"styled-by-prettify">move</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify">std</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy">unique_lock</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">&lt;</span><span style=3D"color: #000;" class=3D"styled-by-prettify">s=
td</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::</span=
><span style=3D"color: #000;" class=3D"styled-by-prettify">mutex</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">&gt;(</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify">m</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">)))</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br>&nbsp; &nbsp; </span><span style=3D"color: #800;" class=
=3D"styled-by-prettify">// i think this would work... but ewww</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-by-prettify"><br></span></div></code></div><br><br=
>I think this would probably work, but it's ugly. Simply adding the ability=
 for the constructor syntax to be allowed when an explicit operator bool is=
 present makes for some very nice and simply syntax. And personally, I thin=
k it reads a little easier too :-)<div><br></div><div>Thoughts/Comments/Cri=
ticisms?</div><div>Evan</div></div>

<p></p>

-- <br />
&nbsp;<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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<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_155_415361.1390765370748--

.


Author: Miro Knejp <miro@knejp.de>
Date: Sun, 26 Jan 2014 20:53:28 +0100
Raw View
This is a multi-part message in MIME format.
--------------090909050602010201010000
Content-Type: text/plain; charset=UTF-8; format=flowed


Am 26.01.2014 20:42, schrieb Evan Teran:
>
> I love that we can write things like this:
>
> |
> if(autop =dynamic_cast<T *>(x)){
> // use p here
> }
> |
>
> it is impossible to use p when the cast fails because it doesn't exist
> outside the scope of the if. One thing that I've wished we could do
> was to do the same, but with constructor syntax. So for example, I'd
> like to be able to do this:
>
>
> |
> if(T x(y)){
> // use x here
> }
> |
>
if(auto x = T{y}) {
     // use x here
}

There's no difference between p, a pointer, and x, a compound type with
ctor/dtor and everything. operator= (should) return a reference to the
left operand (unless you're evil), which can then be converted to bool
given a suitable conversion. The lifetime of x extends to the end of the
if statement and that includes the entire { } block. The compiler does
an implicit move construction for x if T supoprts it and destroys it
when execution leaves the statement block. All types are equal in that
regard.

Unless my brain just went horribly wrong what you're asking for is
already there.

--

---
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/.

--------------090909050602010201010000
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<html>
  <head>
    <meta content=3D"text/html; charset=3DUTF-8" http-equiv=3D"Content-Type=
">
  </head>
  <body text=3D"#000000" bgcolor=3D"#FFFFFF">
    <br>
    <div class=3D"moz-cite-prefix">Am 26.01.2014 20:42, schrieb Evan
      Teran:<br>
    </div>
    <blockquote
      cite=3D"mid:c5172708-5c70-4333-8bb6-66badb5ec813@isocpp.org"
      type=3D"cite">
      <div dir=3D"ltr"><br>
        <div>I love that we can write things like this:</div>
        <div><br>
        </div>
        <div class=3D"prettyprint" style=3D"background-color: rgb(250, 250,
          250); border: 1px solid rgb(187, 187, 187); word-wrap:
          break-word;"><code class=3D"prettyprint">
            <div class=3D"subprettyprint"><span style=3D"color: #008;"
                class=3D"styled-by-prettify">if</span><span style=3D"color:
                #660;" class=3D"styled-by-prettify">(</span><span
                style=3D"color: #008;" class=3D"styled-by-prettify">auto</s=
pan><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> p </sp=
an><span
                style=3D"color: #660;" class=3D"styled-by-prettify">=3D</sp=
an><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #008;" class=3D"styled-by-prettify">dynamic=
_cast</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">&lt;</s=
pan><span
                style=3D"color: #000;" class=3D"styled-by-prettify">T </spa=
n><span
                style=3D"color: #660;" class=3D"styled-by-prettify">*&gt;(<=
/span><span
                style=3D"color: #000;" class=3D"styled-by-prettify">x</span=
><span
                style=3D"color: #660;" class=3D"styled-by-prettify">))</spa=
n><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>
                =C2=A0 =C2=A0 </span><span style=3D"color: #800;"
                class=3D"styled-by-prettify">// use p here</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
              </span><span style=3D"color: #660;"
                class=3D"styled-by-prettify">}</span></div>
          </code></div>
        <div><br>
        </div>
        <div>it is impossible to use p when the cast fails because it
          doesn't exist outside the scope of the if. One thing that I've
          wished we could do was to do the same, but with constructor
          syntax. So for example, I'd like to be able to do this:</div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div class=3D"prettyprint" style=3D"background-color: rgb(250, 250,
          250); border: 1px solid rgb(187, 187, 187); word-wrap:
          break-word;"><code class=3D"prettyprint">
            <div class=3D"subprettyprint"><span style=3D"color: #008;"
                class=3D"styled-by-prettify">if</span><span style=3D"color:
                #660;" class=3D"styled-by-prettify">(</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify">T 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">))</spa=
n><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>
                =C2=A0 =C2=A0 </span><span style=3D"color: #800;"
                class=3D"styled-by-prettify">// use x here</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
              </span><span style=3D"color: #660;"
                class=3D"styled-by-prettify">}</span></div>
          </code></div>
        <div><br>
        </div>
      </div>
    </blockquote>
    if(auto x =3D T{y}) {<br>
    =C2=A0=C2=A0=C2=A0 // use x here<br>
    }<br>
    <br>
    There's no difference between p, a pointer, and x, a compound type
    with ctor/dtor and everything. operator=3D (should) return a reference
    to the left operand (unless you're evil), which can then be
    converted to bool given a suitable conversion. The lifetime of x
    extends to the end of the if statement and that includes the entire
    { } block. The compiler does an implicit move construction for x if
    T supoprts it and destroys it when execution leaves the statement
    block. All types are equal in that regard.<br>
    <br>
    Unless my brain just went horribly wrong what you're asking for is
    already there.<br>
    <br>
  </body>
</html>

<p></p>

-- <br />
&nbsp;<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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<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 />

--------------090909050602010201010000--


.


Author: Evan Teran <evan.teran@gmail.com>
Date: Sun, 26 Jan 2014 12:30:49 -0800 (PST)
Raw View
------=_Part_2641_15693109.1390768249182
Content-Type: text/plain; charset=UTF-8

I hadn't really considered the use of auto. it definitely comes close to
what I'm asking for. One subtle difference is that what I'm suggesting
would likely allow the scoped object to be unnamed. So while we could write
this:

if(auto l = std::unique_lock<std::mutex>(m)) {

}


Which is very nice... I'd like to be able to write this which is slightly
more terse.

if(std::unique_lock<std::mutex>(m)) {


}

If I only need the variable to exist for a period of time, but don't have a
need to access it.

Like I said, I hadn't thought of using auto here. Which gets me a lot of
what I would like. So if my suggestion goes nowhere, I'll get over it. I
still feel there may be a case for my suggestion though.
Evan

On Sunday, January 26, 2014 2:53:28 PM UTC-5, Miro Knejp wrote:
>
>
> Am 26.01.2014 20:42, schrieb Evan Teran:
>
>
> I love that we can write things like this:
>
>  if(auto p = dynamic_cast<T *>(x)) {
>     // use p here
> }
>
>  it is impossible to use p when the cast fails because it doesn't exist
> outside the scope of the if. One thing that I've wished we could do was to
> do the same, but with constructor syntax. So for example, I'd like to be
> able to do this:
>
>
>  if(T x(y)) {
>     // use x here
> }
>
>   if(auto x = T{y}) {
>     // use x here
> }
>
> There's no difference between p, a pointer, and x, a compound type with
> ctor/dtor and everything. operator= (should) return a reference to the left
> operand (unless you're evil), which can then be converted to bool given a
> suitable conversion. The lifetime of x extends to the end of the if
> statement and that includes the entire { } block. The compiler does an
> implicit move construction for x if T supoprts it and destroys it when
> execution leaves the statement block. All types are equal in that regard.
>
> Unless my brain just went horribly wrong what you're asking for is already
> there.
>
>

--

---
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_2641_15693109.1390768249182
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">I hadn't really considered the use of auto. it definitely =
comes close to what I'm asking for. One subtle difference is that what I'm =
suggesting would likely allow the scoped object to be unnamed. So while we =
could write this:<div><br></div><div class=3D"prettyprint" style=3D"backgro=
und-color: rgb(250, 250, 250); border: 1px solid rgb(187, 187, 187); word-w=
rap: break-word;"><code class=3D"prettyprint"><div class=3D"subprettyprint"=
><span style=3D"color: #008;" class=3D"styled-by-prettify">if</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"c=
olor: #008;" class=3D"styled-by-prettify">auto</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> l </span><span style=3D"color: #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"st=
yled-by-prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify">unique_lock</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">&lt;</span><span style=3D"color: #000;" class=3D"styled-by-prett=
ify">std</span><span style=3D"color: #660;" class=3D"styled-by-prettify">::=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify">mutex</spa=
n><span style=3D"color: #660;" class=3D"styled-by-prettify">&gt;(</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify">m</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">))</span><span style=3D"colo=
r: #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><br></span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">}</span></div></code></div><div><br></div><div><br></div=
><div>Which is very nice... I'd like to be able to write this which is slig=
htly more terse.</div><div><br></div><div class=3D"prettyprint" style=3D"ba=
ckground-color: rgb(250, 250, 250); border: 1px solid rgb(187, 187, 187); w=
ord-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"subprettyp=
rint"><span style=3D"color: #008;" class=3D"styled-by-prettify">if</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">std</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify">unique_lock</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">&lt;</span><span style=3D"color: #000;" cl=
ass=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-b=
y-prettify">mutex</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">&gt;(</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
">m</span><span style=3D"color: #660;" class=3D"styled-by-prettify">))</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"><br><br><br></span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">}</span></div></code></div><div=
><span style=3D"font-family: monospace; background-color: rgb(250, 250, 250=
); color: rgb(102, 102, 0);"><br></span></div><div>If I only need the varia=
ble to exist for a period of time, but don't have a need to access it.</div=
><div><br></div><div>Like I said, I hadn't thought of using auto here. Whic=
h gets me a lot of what I would like. So if my suggestion goes nowhere, I'l=
l get over it. I still feel there may be a case for my suggestion though.</=
div><div>Evan<br><br>On Sunday, January 26, 2014 2:53:28 PM UTC-5, Miro Kne=
jp wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: =
0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">
 =20
   =20
 =20
  <div text=3D"#000000" bgcolor=3D"#FFFFFF">
    <br>
    <div>Am 26.01.2014 20:42, schrieb Evan
      Teran:<br>
    </div>
    <blockquote type=3D"cite">
      <div dir=3D"ltr"><br>
        <div>I love that we can write things like this:</div>
        <div><br>
        </div>
        <div style=3D"background-color:rgb(250,250,250);border:1px solid rg=
b(187,187,187);word-wrap:break-word"><code>
            <div><span style=3D"color:#008">if</span><span style=3D"color:#=
660">(</span><span style=3D"color:#008">auto</span><span style=3D"color:#00=
0"> p </span><span style=3D"color:#660">=3D</span><span style=3D"color:#000=
"> </span><span style=3D"color:#008">dynamic_cast</span><span style=3D"colo=
r:#660">&lt;</span><span style=3D"color:#000">T </span><span style=3D"color=
:#660">*&gt;(</span><span style=3D"color:#000">x</span><span style=3D"color=
:#660">))</span><span style=3D"color:#000"> </span><span style=3D"color:#66=
0">{</span><span style=3D"color:#000"><br>
                &nbsp; &nbsp; </span><span style=3D"color:#800">// use p he=
re</span><span style=3D"color:#000"><br>
              </span><span style=3D"color:#660">}</span></div>
          </code></div>
        <div><br>
        </div>
        <div>it is impossible to use p when the cast fails because it
          doesn't exist outside the scope of the if. One thing that I've
          wished we could do was to do the same, but with constructor
          syntax. So for example, I'd like to be able to do this:</div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div style=3D"background-color:rgb(250,250,250);border:1px solid rg=
b(187,187,187);word-wrap:break-word"><code>
            <div><span style=3D"color:#008">if</span><span style=3D"color:#=
660">(</span><span style=3D"color:#000">T x</span><span style=3D"color:#660=
">(</span><span style=3D"color:#000">y</span><span style=3D"color:#660">))<=
/span><span style=3D"color:#000"> </span><span style=3D"color:#660">{</span=
><span style=3D"color:#000"><br>
                &nbsp; &nbsp; </span><span style=3D"color:#800">// use x he=
re</span><span style=3D"color:#000"><br>
              </span><span style=3D"color:#660">}</span></div>
          </code></div>
        <div><br>
        </div>
      </div>
    </blockquote>
    if(auto x =3D T{y}) {<br>
    &nbsp;&nbsp;&nbsp; // use x here<br>
    }<br>
    <br>
    There's no difference between p, a pointer, and x, a compound type
    with ctor/dtor and everything. operator=3D (should) return a reference
    to the left operand (unless you're evil), which can then be
    converted to bool given a suitable conversion. The lifetime of x
    extends to the end of the if statement and that includes the entire
    { } block. The compiler does an implicit move construction for x if
    T supoprts it and destroys it when execution leaves the statement
    block. All types are equal in that regard.<br>
    <br>
    Unless my brain just went horribly wrong what you're asking for is
    already there.<br>
    <br>
  </div>

</blockquote></div></div>

<p></p>

-- <br />
&nbsp;<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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<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_2641_15693109.1390768249182--

.


Author: Maurice Bos <m-ou.se@m-ou.se>
Date: Sun, 26 Jan 2014 22:00:42 +0100
Raw View
--047d7bb04adc8d961c04f0e5e300
Content-Type: text/plain; charset=UTF-8

If (std::unique_lock<std::mutex>(m)) is already valid code. It'd be a bad
idea to *change* what it does. (Now it destructs the temporary unique_locks
directly after evaluating the condition expression.)

Maybe you can go for somethiing like: if (auto = ...)

It happens more often that one wants to define a variable which doesn't
need a name, other than just in if-statement condition expressions. (For
example, when it's only used for RAII.) Maybe a similar syntax (just "auto
= ...;", or something like that) could provide a solution for this.


2014-01-26 Evan Teran <evan.teran@gmail.com>

> I hadn't really considered the use of auto. it definitely comes close to
> what I'm asking for. One subtle difference is that what I'm suggesting
> would likely allow the scoped object to be unnamed. So while we could write
> this:
>
> if(auto l = std::unique_lock<std::mutex>(m)) {
>
> }
>
>
> Which is very nice... I'd like to be able to write this which is slightly
> more terse.
>
> if(std::unique_lock<std::mutex>(m)) {
>
>
> }
>
> If I only need the variable to exist for a period of time, but don't have
> a need to access it.
>
> Like I said, I hadn't thought of using auto here. Which gets me a lot of
> what I would like. So if my suggestion goes nowhere, I'll get over it. I
> still feel there may be a case for my suggestion though.
> Evan
>
>
> On Sunday, January 26, 2014 2:53:28 PM UTC-5, Miro Knejp wrote:
>>
>>
>> Am 26.01.2014 20:42, schrieb Evan Teran:
>>
>>
>> I love that we can write things like this:
>>
>>  if(auto p = dynamic_cast<T *>(x)) {
>>     // use p here
>> }
>>
>>  it is impossible to use p when the cast fails because it doesn't exist
>> outside the scope of the if. One thing that I've wished we could do was to
>> do the same, but with constructor syntax. So for example, I'd like to be
>> able to do this:
>>
>>
>>  if(T x(y)) {
>>     // use x here
>> }
>>
>>   if(auto x = T{y}) {
>>     // use x here
>> }
>>
>> There's no difference between p, a pointer, and x, a compound type with
>> ctor/dtor and everything. operator= (should) return a reference to the left
>> operand (unless you're evil), which can then be converted to bool given a
>> suitable conversion. The lifetime of x extends to the end of the if
>> statement and that includes the entire { } block. The compiler does an
>> implicit move construction for x if T supoprts it and destroys it when
>> execution leaves the statement block. All types are equal in that regard.
>>
>> Unless my brain just went horribly wrong what you're asking for is
>> already there.
>>
>>   --
>
> ---
> 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/.
>

--

---
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/.

--047d7bb04adc8d961c04f0e5e300
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">If (std::unique_lock&lt;std::mutex&gt;(m)) is already vali=
d code. It&#39;d be a bad idea to *change* what it does. (Now it destructs =
the temporary unique_locks directly after evaluating the condition expressi=
on.)<br>

<br>Maybe you can go for somethiing like: if (auto =3D ...)<br><br>It happe=
ns more often that one wants to define a variable which doesn&#39;t need a =
name, other than just in if-statement condition expressions. (For example, =
when it&#39;s only used for RAII.) Maybe a similar syntax (just &quot;auto =
=3D ...;&quot;, or something like that) could provide a solution for this.<=
br>

</div><div class=3D"gmail_extra"><br><br><div class=3D"gmail_quote">2014-01=
-26 Evan Teran <span dir=3D"ltr">&lt;<a href=3D"mailto:evan.teran@gmail.com=
" target=3D"_blank">evan.teran@gmail.com</a>&gt;</span><br><blockquote clas=
s=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;pad=
ding-left:1ex">

<div dir=3D"ltr">I hadn&#39;t really considered the use of auto. it definit=
ely comes close to what I&#39;m asking for. One subtle difference is that w=
hat I&#39;m suggesting would likely allow the scoped object to be unnamed. =
So while we could write this:<div>

<br></div><div style=3D"background-color:rgb(250,250,250);border:1px solid =
rgb(187,187,187);word-wrap:break-word"><code><div><span style=3D"color:#008=
">if</span><span style=3D"color:#660">(</span><span style=3D"color:#008">au=
to</span><span style> l </span><span style=3D"color:#660">=3D</span><span s=
tyle> std</span><span style=3D"color:#660">::</span><span style>unique_lock=
</span><span style=3D"color:#660">&lt;</span><span style>std</span><span st=
yle=3D"color:#660">::</span><span style>mutex</span><span style=3D"color:#6=
60">&gt;(</span><span style>m</span><span style=3D"color:#660">))</span><sp=
an style> </span><span style=3D"color:#660">{</span><span style><br>

<br></span><span style=3D"color:#660">}</span></div></code></div><div><br><=
/div><div><br></div><div>Which is very nice... I&#39;d like to be able to w=
rite this which is slightly more terse.</div><div class=3D"im"><div><br></d=
iv>

<div style=3D"background-color:rgb(250,250,250);border:1px solid rgb(187,18=
7,187);word-wrap:break-word"><code><div><span style=3D"color:#008">if</span=
><span style=3D"color:#660">(</span><span style>std</span><span style=3D"co=
lor:#660">::</span><span style>unique_lock</span><span style=3D"color:#660"=
>&lt;</span><span style>std</span><span style=3D"color:#660">::</span><span=
 style>mutex</span><span style=3D"color:#660">&gt;(</span><span style>m</sp=
an><span style=3D"color:#660">))</span><span style> </span><span style=3D"c=
olor:#660">{</span><span style><br>

<br><br></span><span style=3D"color:#660">}</span></div></code></div><div><=
span style=3D"font-family:monospace;background-color:rgb(250,250,250);color=
:rgb(102,102,0)"><br></span></div></div><div>If I only need the variable to=
 exist for a period of time, but don&#39;t have a need to access it.</div>

<div><br></div><div>Like I said, I hadn&#39;t thought of using auto here. W=
hich gets me a lot of what I would like. So if my suggestion goes nowhere, =
I&#39;ll get over it. I still feel there may be a case for my suggestion th=
ough.</div>

<div><span class=3D"HOEnZb"><font color=3D"#888888">Evan</font></span><div>=
<div class=3D"h5"><br><br>On Sunday, January 26, 2014 2:53:28 PM UTC-5, Mir=
o Knejp wrote:<blockquote class=3D"gmail_quote" style=3D"margin:0;margin-le=
ft:0.8ex;border-left:1px #ccc solid;padding-left:1ex">


 =20
   =20
 =20
  <div text=3D"#000000" bgcolor=3D"#FFFFFF">
    <br>
    <div>Am 26.01.2014 20:42, schrieb Evan
      Teran:<br>
    </div>
    <blockquote type=3D"cite">
      <div dir=3D"ltr"><br>
        <div>I love that we can write things like this:</div>
        <div><br>
        </div>
        <div style=3D"background-color:rgb(250,250,250);border:1px solid rg=
b(187,187,187);word-wrap:break-word"><code>
            <div><span style=3D"color:#008">if</span><span style=3D"color:#=
660">(</span><span style=3D"color:#008">auto</span><span style> p </span><s=
pan style=3D"color:#660">=3D</span><span style> </span><span style=3D"color=
:#008">dynamic_cast</span><span style=3D"color:#660">&lt;</span><span style=
>T </span><span style=3D"color:#660">*&gt;(</span><span style>x</span><span=
 style=3D"color:#660">))</span><span style> </span><span style=3D"color:#66=
0">{</span><span style><br>


                =C2=A0 =C2=A0 </span><span style=3D"color:#800">// use p he=
re</span><span style><br>
              </span><span style=3D"color:#660">}</span></div>
          </code></div>
        <div><br>
        </div>
        <div>it is impossible to use p when the cast fails because it
          doesn&#39;t exist outside the scope of the if. One thing that I&#=
39;ve
          wished we could do was to do the same, but with constructor
          syntax. So for example, I&#39;d like to be able to do this:</div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div style=3D"background-color:rgb(250,250,250);border:1px solid rg=
b(187,187,187);word-wrap:break-word"><code>
            <div><span style=3D"color:#008">if</span><span style=3D"color:#=
660">(</span><span style>T x</span><span style=3D"color:#660">(</span><span=
 style>y</span><span style=3D"color:#660">))</span><span style> </span><spa=
n style=3D"color:#660">{</span><span style><br>


                =C2=A0 =C2=A0 </span><span style=3D"color:#800">// use x he=
re</span><span style><br>
              </span><span style=3D"color:#660">}</span></div>
          </code></div>
        <div><br>
        </div>
      </div>
    </blockquote>
    if(auto x =3D T{y}) {<br>
    =C2=A0=C2=A0=C2=A0 // use x here<br>
    }<br>
    <br>
    There&#39;s no difference between p, a pointer, and x, a compound type
    with ctor/dtor and everything. operator=3D (should) return a reference
    to the left operand (unless you&#39;re evil), which can then be
    converted to bool given a suitable conversion. The lifetime of x
    extends to the end of the if statement and that includes the entire
    { } block. The compiler does an implicit move construction for x if
    T supoprts it and destroys it when execution leaves the statement
    block. All types are equal in that regard.<br>
    <br>
    Unless my brain just went horribly wrong what you&#39;re asking for is
    already there.<br>
    <br>
  </div>

</blockquote></div></div></div></div><div class=3D"HOEnZb"><div class=3D"h5=
">

<p></p>

-- <br>
=C2=A0<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%2Bunsubscribe@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>

<p></p>

-- <br />
&nbsp;<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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<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 />

--047d7bb04adc8d961c04f0e5e300--

.


Author: Evan Teran <evan.teran@gmail.com>
Date: Sun, 26 Jan 2014 18:20:24 -0800 (PST)
Raw View
------=_Part_693_23340469.1390789224673
Content-Type: text/plain; charset=UTF-8

Ah, good point, I supposed I should have looked to see if that code already
had well defined behavior :-/. I think I'd be on board for a syntax like
you suggest:

if(auto = std::unique_lock<std::mutex>(m)) {
}

But like i said, now that it has been pointed out that I can just use auto,
I'm at least content with the solution that Miro pointed out. Not as pretty
as I'd like, but equally functional.


On Sunday, January 26, 2014 4:00:42 PM UTC-5, Maurice Bos wrote:
>
> If (std::unique_lock<std::mutex>(m)) is already valid code. It'd be a bad
> idea to *change* what it does. (Now it destructs the temporary unique_locks
> directly after evaluating the condition expression.)
>
> Maybe you can go for somethiing like: if (auto = ...)
>
> It happens more often that one wants to define a variable which doesn't
> need a name, other than just in if-statement condition expressions. (For
> example, when it's only used for RAII.) Maybe a similar syntax (just "auto
> = ...;", or something like that) could provide a solution for this.
>
>
> 2014-01-26 Evan Teran <evan....@gmail.com <javascript:>>
>
>> I hadn't really considered the use of auto. it definitely comes close to
>> what I'm asking for. One subtle difference is that what I'm suggesting
>> would likely allow the scoped object to be unnamed. So while we could write
>> this:
>>
>> if(auto l = std::unique_lock<std::mutex>(m)) {
>>
>> }
>>
>>
>> Which is very nice... I'd like to be able to write this which is slightly
>> more terse.
>>
>> if(std::unique_lock<std::mutex>(m)) {
>>
>>
>> }
>>
>> If I only need the variable to exist for a period of time, but don't have
>> a need to access it.
>>
>> Like I said, I hadn't thought of using auto here. Which gets me a lot of
>> what I would like. So if my suggestion goes nowhere, I'll get over it. I
>> still feel there may be a case for my suggestion though.
>> Evan
>>
>>
>> On Sunday, January 26, 2014 2:53:28 PM UTC-5, Miro Knejp wrote:
>>>
>>>
>>> Am 26.01.2014 20:42, schrieb Evan Teran:
>>>
>>>
>>> I love that we can write things like this:
>>>
>>>  if(auto p = dynamic_cast<T *>(x)) {
>>>     // use p here
>>> }
>>>
>>>  it is impossible to use p when the cast fails because it doesn't exist
>>> outside the scope of the if. One thing that I've wished we could do was to
>>> do the same, but with constructor syntax. So for example, I'd like to be
>>> able to do this:
>>>
>>>
>>>  if(T x(y)) {
>>>     // use x here
>>> }
>>>
>>>   if(auto x = T{y}) {
>>>     // use x here
>>> }
>>>
>>> There's no difference between p, a pointer, and x, a compound type with
>>> ctor/dtor and everything. operator= (should) return a reference to the left
>>> operand (unless you're evil), which can then be converted to bool given a
>>> suitable conversion. The lifetime of x extends to the end of the if
>>> statement and that includes the entire { } block. The compiler does an
>>> implicit move construction for x if T supoprts it and destroys it when
>>> execution leaves the statement block. All types are equal in that regard.
>>>
>>> Unless my brain just went horribly wrong what you're asking for is
>>> already there.
>>>
>>>   --
>>
>> ---
>> 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-proposal...@isocpp.org <javascript:>.
>> To post to this group, send email to std-pr...@isocpp.org <javascript:>.
>> Visit this group at
>> http://groups.google.com/a/isocpp.org/group/std-proposals/.
>>
>
>

--

---
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_693_23340469.1390789224673
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Ah, good point, I supposed I should have looked to see if =
that code already had well defined behavior :-/. I think I'd be on board fo=
r a syntax like you suggest:<div><br></div><div class=3D"prettyprint" style=
=3D"background-color: rgb(250, 250, 250); border: 1px solid rgb(187, 187, 1=
87); word-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"subp=
rettyprint"><span style=3D"color: #008;" class=3D"styled-by-prettify">if</s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span=
 style=3D"color: #008;" class=3D"styled-by-prettify">auto</span><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: #000;=
" class=3D"styled-by-prettify"> std</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">::</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify">unique_lock</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">&lt;</span><span style=3D"color: #000;" class=3D"styled-b=
y-prettify">std</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">::</span><span style=3D"color: #000;" class=3D"styled-by-prettify">mut=
ex</span><span style=3D"color: #660;" class=3D"styled-by-prettify">&gt;(</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify">m</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: #660;" class=
=3D"styled-by-prettify">}</span></div></code></div><div><br></div><div>But =
like i said, now that it has been pointed out that I can just use auto, I'm=
 at least content with the solution that Miro pointed out. Not as pretty as=
 I'd like, but equally functional.<br><br><br>On Sunday, January 26, 2014 4=
:00:42 PM UTC-5, Maurice Bos 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">If (std::unique_lock&lt;std::mutex&gt;(<wbr>m)) is a=
lready valid code. It'd be a bad idea to *change* what it does. (Now it des=
tructs the temporary unique_locks directly after evaluating the condition e=
xpression.)<br>

<br>Maybe you can go for somethiing like: if (auto =3D ...)<br><br>It happe=
ns more often that one wants to define a variable which doesn't need a name=
, other than just in if-statement condition expressions. (For example, when=
 it's only used for RAII.) Maybe a similar syntax (just "auto =3D ...;", or=
 something like that) could provide a solution for this.<br>

</div><div><br><br><div class=3D"gmail_quote">2014-01-26 Evan Teran <span d=
ir=3D"ltr">&lt;<a href=3D"javascript:" target=3D"_blank" gdf-obfuscated-mai=
lto=3D"qyZd9UJHiZAJ" onmousedown=3D"this.href=3D'javascript:';return true;"=
 onclick=3D"this.href=3D'javascript:';return true;">evan....@gmail.com</a>&=
gt;</span><br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;=
border-left:1px #ccc solid;padding-left:1ex">

<div dir=3D"ltr">I hadn't really considered the use of auto. it definitely =
comes close to what I'm asking for. One subtle difference is that what I'm =
suggesting would likely allow the scoped object to be unnamed. So while we =
could write this:<div>

<br></div><div style=3D"background-color:rgb(250,250,250);border:1px solid =
rgb(187,187,187);word-wrap:break-word"><code><div><span style=3D"color:#008=
">if</span><span style=3D"color:#660">(</span><span style=3D"color:#008">au=
to</span><span> l </span><span style=3D"color:#660">=3D</span><span> std</s=
pan><span style=3D"color:#660">::</span><span>unique_lock</span><span style=
=3D"color:#660">&lt;</span><span>std</span><span style=3D"color:#660">::</s=
pan><span>mutex</span><span style=3D"color:#660">&gt;(</span><span>m</span>=
<span style=3D"color:#660"><wbr>))</span><span> </span><span style=3D"color=
:#660">{</span><span><br>

<br></span><span style=3D"color:#660">}</span></div></code></div><div><br><=
/div><div><br></div><div>Which is very nice... I'd like to be able to write=
 this which is slightly more terse.</div><div><div><br></div>

<div style=3D"background-color:rgb(250,250,250);border:1px solid rgb(187,18=
7,187);word-wrap:break-word"><code><div><span style=3D"color:#008">if</span=
><span style=3D"color:#660">(</span><span>std</span><span style=3D"color:#6=
60">::</span><span>unique_lock</span><span style=3D"color:#660">&lt;</span>=
<span>std</span><span style=3D"color:#660">::</span><span>mutex</span><span=
 style=3D"color:#660"><wbr>&gt;(</span><span>m</span><span style=3D"color:#=
660">))</span><span> </span><span style=3D"color:#660">{</span><span><br>

<br><br></span><span style=3D"color:#660">}</span></div></code></div><div><=
span style=3D"font-family:monospace;background-color:rgb(250,250,250);color=
:rgb(102,102,0)"><br></span></div></div><div>If I only need the variable to=
 exist for a period of time, but don't have a need to access it.</div>

<div><br></div><div>Like I said, I hadn't thought of using auto here. Which=
 gets me a lot of what I would like. So if my suggestion goes nowhere, I'll=
 get over it. I still feel there may be a case for my suggestion though.</d=
iv>

<div><span><font color=3D"#888888">Evan</font></span><div><div><br><br>On S=
unday, January 26, 2014 2:53:28 PM UTC-5, Miro Knejp wrote:<blockquote clas=
s=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc =
solid;padding-left:1ex">


 =20
   =20
 =20
  <div text=3D"#000000" bgcolor=3D"#FFFFFF">
    <br>
    <div>Am 26.01.2014 20:42, schrieb Evan
      Teran:<br>
    </div>
    <blockquote type=3D"cite">
      <div dir=3D"ltr"><br>
        <div>I love that we can write things like this:</div>
        <div><br>
        </div>
        <div style=3D"background-color:rgb(250,250,250);border:1px solid rg=
b(187,187,187);word-wrap:break-word"><code>
            <div><span style=3D"color:#008">if</span><span style=3D"color:#=
660">(</span><span style=3D"color:#008">auto</span><span> p </span><span st=
yle=3D"color:#660">=3D</span><span> </span><span style=3D"color:#008">dynam=
ic_cast</span><span style=3D"color:#660">&lt;</span><span>T </span><span st=
yle=3D"color:#660">*&gt;(</span><span>x</span><span style=3D"color:#660">))=
</span><span> </span><span style=3D"color:#660">{</span><span><br>


                &nbsp; &nbsp; </span><span style=3D"color:#800">// use p he=
re</span><span><br>
              </span><span style=3D"color:#660">}</span></div>
          </code></div>
        <div><br>
        </div>
        <div>it is impossible to use p when the cast fails because it
          doesn't exist outside the scope of the if. One thing that I've
          wished we could do was to do the same, but with constructor
          syntax. So for example, I'd like to be able to do this:</div>
        <div><br>
        </div>
        <div><br>
        </div>
        <div style=3D"background-color:rgb(250,250,250);border:1px solid rg=
b(187,187,187);word-wrap:break-word"><code>
            <div><span style=3D"color:#008">if</span><span style=3D"color:#=
660">(</span><span>T x</span><span style=3D"color:#660">(</span><span>y</sp=
an><span style=3D"color:#660">))</span><span> </span><span style=3D"color:#=
660">{</span><span><br>


                &nbsp; &nbsp; </span><span style=3D"color:#800">// use x he=
re</span><span><br>
              </span><span style=3D"color:#660">}</span></div>
          </code></div>
        <div><br>
        </div>
      </div>
    </blockquote>
    if(auto x =3D T{y}) {<br>
    &nbsp;&nbsp;&nbsp; // use x here<br>
    }<br>
    <br>
    There's no difference between p, a pointer, and x, a compound type
    with ctor/dtor and everything. operator=3D (should) return a reference
    to the left operand (unless you're evil), which can then be
    converted to bool given a suitable conversion. The lifetime of x
    extends to the end of the if statement and that includes the entire
    { } block. The compiler does an implicit move construction for x if
    T supoprts it and destroys it when execution leaves the statement
    block. All types are equal in that regard.<br>
    <br>
    Unless my brain just went horribly wrong what you're asking for is
    already there.<br>
    <br>
  </div>

</blockquote></div></div></div></div><div><div>

<p></p>

-- <br>
&nbsp;<br>
--- <br>
You received this message because you are subscribed to the Google Groups "=
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"javascript:" target=3D"_blank" gdf-obfuscated-mailto=3D"=
qyZd9UJHiZAJ" onmousedown=3D"this.href=3D'javascript:';return true;" onclic=
k=3D"this.href=3D'javascript:';return true;">std-proposal...@<wbr>isocpp.or=
g</a>.<br>
To post to this group, send email to <a href=3D"javascript:" target=3D"_bla=
nk" gdf-obfuscated-mailto=3D"qyZd9UJHiZAJ" onmousedown=3D"this.href=3D'java=
script:';return true;" onclick=3D"this.href=3D'javascript:';return true;">s=
td-pr...@isocpp.org</a>.<br>
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/" target=3D"_blank" onmousedown=3D"this.href=3D'http://groups=
..google.com/a/isocpp.org/group/std-proposals/';return true;" onclick=3D"thi=
s.href=3D'http://groups.google.com/a/isocpp.org/group/std-proposals/';retur=
n true;">http://groups.google.com/a/<wbr>isocpp.org/group/std-<wbr>proposal=
s/</a>.<br>
</div></div></blockquote></div><br></div>
</blockquote></div></div>

<p></p>

-- <br />
&nbsp;<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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<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_693_23340469.1390789224673--

.


Author: Matthew Woehlke <mw_triad@users.sourceforge.net>
Date: Mon, 27 Jan 2014 12:35:55 -0500
Raw View
On 2014-01-26 16:00, Maurice Bos wrote:
> If (std::unique_lock<std::mutex>(m)) is already valid code. It'd be a bad
> idea to *change* what it does. (Now it destructs the temporary unique_locks
> directly after evaluating the condition expression.)
>
> Maybe you can go for somethiing like: if (auto = ...)

This would be nice. Actually, in general I could make a case for
nameless local variables. For example, maybe an entire class method
needs to hold some RAII resource (or similar construct) that is always
attainable. (Actually, I very frequently need to temporarily set state
on objects - recursion guards, update deferrals, etc. - that could use
this syntax.)

I feel like it should also work for types other than "auto", even if I
can't think of why modern code should ever use such :-).

--
Matthew

--

---
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/.

.


Author: Nicola Gigante <nicola.gigante@gmail.com>
Date: Mon, 27 Jan 2014 18:58:09 +0100
Raw View
Il giorno 27/gen/2014, alle ore 18:35, Matthew Woehlke <mw_triad@users.sour=
ceforge.net> ha scritto:

>=20
> This would be nice. Actually, in general I could make a case for nameless=
 local variables. For example, maybe an entire class method needs to hold s=
ome RAII resource (or similar construct) that is always attainable. (Actual=
ly, I very frequently need to temporarily set state on objects - recursion =
guards, update deferrals, etc. - that could use this syntax.)
>=20
> I feel like it should also work for types other than "auto", even if I ca=
n't think of why modern code should ever use such :-).
>=20

Hello. As you suggest, a use case for some kind of support for nameless loc=
al variable can be the implementation of a macro like
Andrei Alexandrescu=92s =93scope_exit=94 (see [1] for example), where he=92=
s forced to use the non-standard (even if de-facto portable)
__COUNTER__ macro to assign different names to variables declared by differ=
ent instantiation of the same macro:

#define scope_exit \
  auto CONCAT(guard_, __COUNTER__) =3D // etc=85

However, for this specific use case I would rather standardize __COUNTER__.=
...

Anyway, I don=92t see very well a syntax like =93auto =3D expr;=94. Usually=
, there=92s some kind of simmetricity between these two declaration
styles:

T var =3D expr;
T var(expr);

But the simmetricity is lost with that syntax:

T =3D expr; // an unnamed variable that lives with the scope
T(expr); // an expression that creates a temporary that is immediately dest=
royed.

Anyway, a similar functionality can be simulated with unnamed function para=
meters with a default argument (even if this
of course doesn=92t work for other block scopes.

> --=20
> Matthew

Bye,
Nicola

[1] C++ and Beyond 2012 - Systematic Error Handling in C++11 -=20
   http://channel9.msdn.com/Shows/Going+Deep/C-and-Beyond-2012-Andrei-Alexa=
ndrescu-Systematic-Error-Handling-in-C

--=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: Johannes Schaub <schaub.johannes@googlemail.com>
Date: Mon, 27 Jan 2014 19:27:26 +0100
Raw View
2014-01-27 Nicola Gigante <nicola.gigante@gmail.com>:
>
> Il giorno 27/gen/2014, alle ore 18:35, Matthew Woehlke <mw_triad@users.so=
urceforge.net> ha scritto:
>
>>
>> This would be nice. Actually, in general I could make a case for nameles=
s local variables. For example, maybe an entire class method needs to hold =
some RAII resource (or similar construct) that is always attainable. (Actua=
lly, I very frequently need to temporarily set state on objects - recursion=
 guards, update deferrals, etc. - that could use this syntax.)
>>
>> I feel like it should also work for types other than "auto", even if I c=
an't think of why modern code should ever use such :-).
>>
>
> Hello. As you suggest, a use case for some kind of support for nameless l=
ocal variable can be the implementation of a macro like
> Andrei Alexandrescu's "scope_exit" (see [1] for example), where he's forc=
ed to use the non-standard (even if de-facto portable)
> __COUNTER__ macro to assign different names to variables declared by diff=
erent instantiation of the same macro:
>
> #define scope_exit \
>   auto CONCAT(guard_, __COUNTER__) =3D // etc...
>
> However, for this specific use case I would rather standardize __COUNTER_=
_...
>

Alternative macros are formulated at
http://stackoverflow.com/questions/2419650/c-c-macro-template-blackmagic-to=
-generate-unique-name/2419715#
.. For example

FOR_BLOCK(guard) {
  ...
}

--=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: Johannes Schaub <schaub.johannes@googlemail.com>
Date: Mon, 27 Jan 2014 19:28:48 +0100
Raw View
2014-01-27 Johannes Schaub <schaub.johannes@googlemail.com>:
> 2014-01-27 Nicola Gigante <nicola.gigante@gmail.com>:
>>
>> Il giorno 27/gen/2014, alle ore 18:35, Matthew Woehlke <mw_triad@users.s=
ourceforge.net> ha scritto:
>>
>>>
>>> This would be nice. Actually, in general I could make a case for namele=
ss local variables. For example, maybe an entire class method needs to hold=
 some RAII resource (or similar construct) that is always attainable. (Actu=
ally, I very frequently need to temporarily set state on objects - recursio=
n guards, update deferrals, etc. - that could use this syntax.)
>>>
>>> I feel like it should also work for types other than "auto", even if I =
can't think of why modern code should ever use such :-).
>>>
>>
>> Hello. As you suggest, a use case for some kind of support for nameless =
local variable can be the implementation of a macro like
>> Andrei Alexandrescu's "scope_exit" (see [1] for example), where he's for=
ced to use the non-standard (even if de-facto portable)
>> __COUNTER__ macro to assign different names to variables declared by dif=
ferent instantiation of the same macro:
>>
>> #define scope_exit \
>>   auto CONCAT(guard_, __COUNTER__) =3D // etc...
>>
>> However, for this specific use case I would rather standardize __COUNTER=
__...
>>
>
> Alternative macros are formulated at
> http://stackoverflow.com/questions/2419650/c-c-macro-template-blackmagic-=
to-generate-unique-name/2419715#
> . For example
>
> FOR_BLOCK(guard) {
>   ...
> }

Sorry. Here is the correct URL I wanted to link,
http://stackoverflow.com/a/2419715/34509

--=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: Miro Knejp <miro@knejp.de>
Date: Mon, 27 Jan 2014 19:40:36 +0100
Raw View
Am 27.01.2014 18:58, schrieb Nicola Gigante:
> Il giorno 27/gen/2014, alle ore 18:35, Matthew Woehlke <mw_triad@users.so=
urceforge.net> ha scritto:
>
>> This would be nice. Actually, in general I could make a case for nameles=
s local variables. For example, maybe an entire class method needs to hold =
some RAII resource (or similar construct) that is always attainable. (Actua=
lly, I very frequently need to temporarily set state on objects - recursion=
 guards, update deferrals, etc. - that could use this syntax.)
>>
>> I feel like it should also work for types other than "auto", even if I c=
an't think of why modern code should ever use such :-).
>>
> Hello. As you suggest, a use case for some kind of support for nameless l=
ocal variable can be the implementation of a macro like
> Andrei Alexandrescu's "scope_exit" (see [1] for example), where he's forc=
ed to use the non-standard (even if de-facto portable)
> __COUNTER__ macro to assign different names to variables declared by diff=
erent instantiation of the same macro:
>
> #define scope_exit \
>    auto CONCAT(guard_, __COUNTER__) =3D // etc...
>
> However, for this specific use case I would rather standardize __COUNTER_=
_...
>
>
That can be problematic as __COUNTER__ has the potential of breaking the=20
ODR if it's used in headers. It's possible for two translation units to=20
come up with different names for variables, functions, types, etc if=20
they don't use the macro the same number of times in the same places.=20
Just something to be aware of.

--=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: Nicola Gigante <nicola.gigante@gmail.com>
Date: Mon, 27 Jan 2014 21:45:40 +0100
Raw View
--Apple-Mail=_DB0DA562-4CDF-4A8F-BE09-B82F9AF4F406
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=windows-1252


Il giorno 27/gen/2014, alle ore 19:28, Johannes Schaub <schaub.johannes@goo=
glemail.com> ha scritto:
>>=20
>> Alternative macros are formulated at
>> http://stackoverflow.com/questions/2419650/c-c-macro-template-blackmagic=
-to-generate-unique-name/2419715#
>> . For example
>>=20
>> FOR_BLOCK(guard) {
>>  ...
>> }
>=20
> Sorry. Here is the correct URL I wanted to link,
> http://stackoverflow.com/a/2419715/34509

It=92s interesting of course, but actually it doesn=92t do the same thing.

{ // scope A
   FOR_BLOCK(guard) { // scope B
       // code
   }
 =20
  scope_exit { // block C
      // cleanup code
  };
}

The solution you linked declares an object that is visible inside scope B, =
and get destroyed when
scope B exits. The Alexandrescu=92s macro declares a variable _in scope A_,=
 to make sure that
cleanup code in block C is executed at the exit of _scope A_. Of course, th=
e two solve the same problem.

However, the second is cleaner (it=92s a matter of taste, of course), becau=
se you don=92t have to
delegate the cleanup code in methods declared in a class declared elsewhere=
 (if you are using
a C API such as OpenGL or CoreFoundation, you don=92t want to implement a c=
lass for every
push/pop and create/release pair of functions you are going to use).

I acknowledge the ODR problems that could arise from __COUNTER__ as pointed=
 by Miro, but
they do nothing but increasing the number of reasons for a standardized way=
 to have unnamed vars or
obtain unique identifiers in a safe way.

Anyway I think this is going off topic, so don=92t bother=85

Bye,
Nicola

--=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/.

--Apple-Mail=_DB0DA562-4CDF-4A8F-BE09-B82F9AF4F406
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=windows-1252

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html charset=
=3Dwindows-1252"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-=
mode: space; -webkit-line-break: after-white-space;"><br><div><div>Il giorn=
o 27/gen/2014, alle ore 19:28, Johannes Schaub &lt;<a href=3D"mailto:schaub=
..johannes@googlemail.com">schaub.johannes@googlemail.com</a>&gt; ha scritto=
:</div><blockquote type=3D"cite"><div style=3D"font-size: 12px; font-style:=
 normal; font-variant: normal; font-weight: normal; letter-spacing: normal;=
 line-height: normal; orphans: auto; text-align: start; text-indent: 0px; t=
ext-transform: none; white-space: normal; widows: auto; word-spacing: 0px; =
-webkit-text-stroke-width: 0px;"><blockquote type=3D"cite"><br>Alternative =
macros are formulated at<br><a href=3D"http://stackoverflow.com/questions/2=
419650/c-c-macro-template-blackmagic-to-generate-unique-name/2419715#">http=
://stackoverflow.com/questions/2419650/c-c-macro-template-blackmagic-to-gen=
erate-unique-name/2419715#</a><br>. For example<br><br>FOR_BLOCK(guard) {<b=
r>&nbsp;...<br>}<br></blockquote><br>Sorry. Here is the correct URL I wante=
d to link,<br><a href=3D"http://stackoverflow.com/a/2419715/34509">http://s=
tackoverflow.com/a/2419715/34509</a><br></div></blockquote></div><br><div>I=
t=92s interesting of course, but actually it doesn=92t do the same thing.</=
div><div><br></div><div>{ // scope A</div><div>&nbsp; &nbsp;FOR_BLOCK(guard=
) { // scope B</div><div>&nbsp; &nbsp; &nbsp; &nbsp;// code</div><div>&nbsp=
; &nbsp;}</div><div>&nbsp;&nbsp;</div><div>&nbsp; scope_exit { // block C</=
div><div>&nbsp; &nbsp; &nbsp; // cleanup code</div><div>&nbsp; };</div><div=
>}</div><div><br></div><div>The solution you linked declares an object that=
 is visible inside scope B, and get destroyed when</div><div>scope B exits.=
 The Alexandrescu=92s macro declares a variable _in scope A_, to make sure =
that</div><div>cleanup code in block C is executed at the exit of _scope A_=
.. Of course, the two solve the same problem.</div><div><br></div><div>Howev=
er, the second is cleaner (it=92s a matter of taste, of course), because yo=
u don=92t have to</div><div>delegate the cleanup code in methods declared i=
n a class declared elsewhere (if you are using</div><div>a C API such as Op=
enGL or CoreFoundation, you don=92t want to implement a class for every</di=
v><div>push/pop and create/release pair of functions you are going to use).=
</div><div><br></div><div>I acknowledge the ODR problems that could arise f=
rom __COUNTER__ as pointed by Miro, but</div><div>they do nothing but incre=
asing the number of reasons for a standardized way to have unnamed vars or<=
/div><div>obtain unique identifiers in a safe way.</div><div><br></div><div=
>Anyway I think this is going off topic, so don=92t bother=85</div><div><br=
></div><div>Bye,</div><div>Nicola</div></body></html>

<p></p>

-- <br />
&nbsp;<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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<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 />

--Apple-Mail=_DB0DA562-4CDF-4A8F-BE09-B82F9AF4F406--

.