Topic: with' revisited


Author: Matthew Woehlke <mw_triad@users.sourceforge.net>
Date: Fri, 03 Oct 2014 18:22:02 -0400
Raw View
The (somewhat not so) recent thread on local variable scope reminded me
of this again... it's come up a few times to have something like
Python's 'with' keyword.

AFAIK that stalled out without getting anywhere.

What do folks thing about:

  using (=C2=ABstatement-list=C2=BB)
    =C2=ABstatement=C2=BB

....which effectively expands to:

  {
    PRESERVE(=C2=ABstatement-list=C2=BB)
    =C2=ABstatement=C2=BB
  }

....where "PRESERVE" is a 'magic function' that delays destruction of any
rvalues present in =C2=ABstatement-list=C2=BB until the end of the enclosin=
g
scope. (I picked "using" instead of "with" because it a) seems
reasonable and b) would avoid adding a new keyword, but I'm not wedded
to it.)

=C2=ABstatement=C2=BB can be simple ("foo();") or compound ("{ foo(); bar()=
; }").
=C2=ABstatement-list=C2=BB is defined as =C2=ABsimple-statement=C2=BB[; =C2=
=ABstatement-list=C2=BB]
(IOW, "auto a =3D foo()", "foo(); bar()", etc.)

This would also mitigate the desire for anonymous variables.



Here's some "concrete" examples:

  // Example 1
  using (std::lock_guard(mutex))
    synchronized_operation();

  // Example 2
  using (gl::enabled(GL_BLEND);
         gl::enabled(GL_DEPTH_TEST);
         gl::bindShader(myShader))
  {
    // ...code to render the scene...
  }

  // Example 3
  using (auto fp =3D OpenFile(path))
  {
    fp.write("Hello, world!");
    fp.write(additional_data);
  }

  // Example 4
  #define synchronized(lock) using (std::lock_guard(lock))

  // Example 5
  using (std::unique_ptr<Foo> foo{nullptr})
  {
    if (...)
      foo =3D std::make_unique<Foo>(...);
    ...
  }



It was also requested to allow:

  class Foo { ... };
  Foo::Foo(...) using(std::lock_guard(...)
    : ...initializers...
  {
    ...
  }

....although I personally am less convinced of the need. (OTOH, I think
this would be a use case that is not currently possible, unlike the
others which are mainly syntatic sugar.)

--=20
Matthew


--=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: Zhihao Yuan <zy@miator.net>
Date: Fri, 3 Oct 2014 18:38:40 -0400
Raw View
--089e0115f0480d3ca805048c6592
Content-Type: text/plain; charset=UTF-8

On Fri, Oct 3, 2014 at 6:22 PM, Matthew Woehlke <
mw_triad@users.sourceforge.net> wrote:

>
>   // Example 4
>   #define synchronized(lock) using (std::lock_guard(lock))
>

Exclude this example if you want to write a paper; the transnational
memory TS reserved this keyword.


>
> It was also requested to allow:
>
>   class Foo { ... };
>   Foo::Foo(...) using(std::lock_guard(...)
>     : ...initializers...
>   {
>     ...
>   }
>
> ...although I personally am less convinced of the need. (OTOH, I think
> this would be a use case that is not currently possible, unlike the
> others which are mainly syntatic sugar.)
>

I raised it for consistency.  I can imagine some use cases, like
opening a tempfile for initializing members, and close it after the
object is constructed.

I'm good with the `using` keyword.

--
Zhihao Yuan, ID lichray
The best way to predict the future is to invent it.
___________________________________________________
4BSD -- http://bit.ly/blog4bsd

--

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

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

<div dir=3D"ltr">On Fri, Oct 3, 2014 at 6:22 PM, Matthew Woehlke <span dir=
=3D"ltr">&lt;<a href=3D"mailto:mw_triad@users.sourceforge.net" target=3D"_b=
lank">mw_triad@users.sourceforge.net</a>&gt;</span> wrote:<br><div class=3D=
"gmail_extra"><div class=3D"gmail_quote"><blockquote class=3D"gmail_quote" =
style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br=
>
=C2=A0 // Example 4<br>
=C2=A0 #define synchronized(lock) using (std::lock_guard(lock))<br></blockq=
uote><div><br></div><div>Exclude this example if you want to write a paper;=
 the transnational<br></div><div>memory TS reserved this keyword.<br></div>=
<div>=C2=A0</div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8=
ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
It was also requested to allow:<br>
<br>
=C2=A0 class Foo { ... };<br>
=C2=A0 Foo::Foo(...) using(std::lock_guard(...)<br>
=C2=A0 =C2=A0 : ...initializers...<br>
=C2=A0 {<br>
=C2=A0 =C2=A0 ...<br>
=C2=A0 }<br>
<br>
....although I personally am less convinced of the need. (OTOH, I think<br>
this would be a use case that is not currently possible, unlike the<br>
others which are mainly syntatic sugar.)<br>
<span class=3D"HOEnZb"></span></blockquote><div><br></div><div>I raised it =
for consistency.=C2=A0 I can imagine some use cases, like<br>opening a temp=
file for initializing members, and close it after the<br>object is construc=
ted.<br><br></div><div>I&#39;m good with the `using` keyword.</div><div><br=
></div></div>-- <br>Zhihao Yuan, ID lichray<br>The best way to predict the =
future is to invent it.<br>________________________________________________=
___<br>4BSD -- <a href=3D"http://bit.ly/blog4bsd" target=3D"_blank">http://=
bit.ly/blog4bsd</a>
</div></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

--089e0115f0480d3ca805048c6592--

.


Author: David Krauss <potswa@gmail.com>
Date: Sat, 4 Oct 2014 07:36:44 +0800
Raw View
--Apple-Mail=_B0E9F408-EA10-4894-9540-7D79893C84A1
Content-Type: text/plain; charset=ISO-8859-1


On 2014-10-04, at 6:22 AM, Matthew Woehlke <mw_triad@users.sourceforge.net> wrote:

> ...where "PRESERVE" is a 'magic function' that delays destruction of any
> rvalues present in <<statement-list>> until the end of the enclosing
> scope. (I picked "using" instead of "with" because it a) seems
> reasonable and b) would avoid adding a new keyword, but I'm not wedded
> to it.)

My generalized lifetime extension proposal allows for that, as long as the function is used to initialize a local variable. See the thread, posted a couple days ago.

I'd appreciate any help with adding examples to that paper or getting it into an easily understandable form.

--

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

--Apple-Mail=_B0E9F408-EA10-4894-9540-7D79893C84A1
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=ISO-8859-1

<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>On 2014&=
ndash;10&ndash;04, at 6:22 AM, Matthew Woehlke &lt;<a href=3D"mailto:mw_tri=
ad@users.sourceforge.net">mw_triad@users.sourceforge.net</a>&gt; wrote:</di=
v><br class=3D"Apple-interchange-newline"><blockquote type=3D"cite"><span s=
tyle=3D"font-family: Helvetica; font-size: 12px; font-style: normal; font-v=
ariant: normal; font-weight: normal; letter-spacing: normal; line-height: n=
ormal; orphans: auto; text-align: start; text-indent: 0px; text-transform: =
none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-st=
roke-width: 0px; float: none; display: inline !important;">...where "PRESER=
VE" is a 'magic function' that delays destruction of any</span><br style=3D=
"font-family: Helvetica; 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; text-transform: none; w=
hite-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-wi=
dth: 0px;"><span style=3D"font-family: Helvetica; font-size: 12px; font-sty=
le: normal; font-variant: normal; font-weight: normal; letter-spacing: norm=
al; line-height: normal; orphans: auto; text-align: start; text-indent: 0px=
; text-transform: none; white-space: normal; widows: auto; word-spacing: 0p=
x; -webkit-text-stroke-width: 0px; float: none; display: inline !important;=
">rvalues present in &laquo;statement-list&raquo; until the end of the encl=
osing</span><br style=3D"font-family: Helvetica; font-size: 12px; font-styl=
e: normal; font-variant: normal; font-weight: normal; letter-spacing: norma=
l; line-height: normal; orphans: auto; text-align: start; text-indent: 0px;=
 text-transform: none; white-space: normal; widows: auto; word-spacing: 0px=
; -webkit-text-stroke-width: 0px;"><span style=3D"font-family: Helvetica; f=
ont-size: 12px; font-style: normal; font-variant: normal; font-weight: norm=
al; letter-spacing: normal; line-height: normal; orphans: auto; text-align:=
 start; text-indent: 0px; text-transform: none; white-space: normal; widows=
: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; dis=
play: inline !important;">scope. (I picked "using" instead of "with" becaus=
e it a) seems</span><br style=3D"font-family: Helvetica; font-size: 12px; f=
ont-style: normal; font-variant: normal; font-weight: normal; letter-spacin=
g: normal; line-height: normal; orphans: auto; text-align: start; text-inde=
nt: 0px; text-transform: none; white-space: normal; widows: auto; word-spac=
ing: 0px; -webkit-text-stroke-width: 0px;"><span style=3D"font-family: Helv=
etica; font-size: 12px; font-style: normal; font-variant: normal; font-weig=
ht: normal; letter-spacing: normal; line-height: normal; orphans: auto; tex=
t-align: start; text-indent: 0px; text-transform: none; white-space: normal=
; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: n=
one; display: inline !important;">reasonable and b) would avoid adding a ne=
w keyword, but I'm not wedded</span><br style=3D"font-family: Helvetica; fo=
nt-size: 12px; font-style: normal; font-variant: normal; font-weight: norma=
l; letter-spacing: normal; line-height: normal; orphans: auto; text-align: =
start; text-indent: 0px; text-transform: none; white-space: normal; widows:=
 auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><span style=3D"f=
ont-family: Helvetica; font-size: 12px; font-style: normal; font-variant: n=
ormal; font-weight: normal; letter-spacing: normal; line-height: normal; or=
phans: auto; text-align: start; text-indent: 0px; text-transform: none; whi=
te-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-widt=
h: 0px; float: none; display: inline !important;">to it.)</span><br style=
=3D"font-family: Helvetica; font-size: 12px; font-style: normal; font-varia=
nt: normal; font-weight: normal; letter-spacing: normal; line-height: norma=
l; orphans: auto; text-align: start; text-indent: 0px; text-transform: none=
; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke=
-width: 0px;"></blockquote></div><br><div>My generalized lifetime extension=
 proposal allows for that, as long as the function is used to initialize a =
local variable. See the thread, posted a couple days ago.</div><div><br></d=
iv><div>I&rsquo;d appreciate any help with adding examples to that paper or=
 getting it into an easily understandable form.</div></body></html>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

--Apple-Mail=_B0E9F408-EA10-4894-9540-7D79893C84A1--

.


Author: Richard Smith <richard@metafoo.co.uk>
Date: Fri, 3 Oct 2014 18:23:20 -0700
Raw View
--047d7b342d32ebb5b805048eb1b8
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

On Fri, Oct 3, 2014 at 3:22 PM, Matthew Woehlke <
mw_triad@users.sourceforge.net> wrote:

> The (somewhat not so) recent thread on local variable scope reminded me
> of this again... it's come up a few times to have something like
> Python's 'with' keyword.
>
> AFAIK that stalled out without getting anywhere.
>
> What do folks thing about:
>
>   using (=C2=ABstatement-list=C2=BB)
>     =C2=ABstatement=C2=BB
>
> ...which effectively expands to:
>
>   {
>     PRESERVE(=C2=ABstatement-list=C2=BB)
>     =C2=ABstatement=C2=BB
>   }
>
> ...where "PRESERVE" is a 'magic function' that delays destruction of any
> rvalues present in =C2=ABstatement-list=C2=BB until the end of the enclos=
ing
> scope. (I picked "using" instead of "with" because it a) seems
> reasonable and b) would avoid adding a new keyword, but I'm not wedded
> to it.)
>
> =C2=ABstatement=C2=BB can be simple ("foo();") or compound ("{ foo(); bar=
(); }").
> =C2=ABstatement-list=C2=BB is defined as =C2=ABsimple-statement=C2=BB[; =
=C2=ABstatement-list=C2=BB]
> (IOW, "auto a =3D foo()", "foo(); bar()", etc.)
>
> This would also mitigate the desire for anonymous variables.
>
>
>
> Here's some "concrete" examples:
>
>   // Example 1
>   using (std::lock_guard(mutex))
>     synchronized_operation();
>
>   // Example 2
>   using (gl::enabled(GL_BLEND);
>          gl::enabled(GL_DEPTH_TEST);
>          gl::bindShader(myShader))
>   {
>     // ...code to render the scene...
>   }
>
>   // Example 3
>   using (auto fp =3D OpenFile(path))
>   {
>     fp.write("Hello, world!");
>     fp.write(additional_data);
>   }
>
>   // Example 4
>   #define synchronized(lock) using (std::lock_guard(lock))
>
>   // Example 5
>   using (std::unique_ptr<Foo> foo{nullptr})
>   {
>     if (...)
>       foo =3D std::make_unique<Foo>(...);
>     ...
>   }
>
>
>
> It was also requested to allow:
>
>   class Foo { ... };
>   Foo::Foo(...) using(std::lock_guard(...)
>     : ...initializers...
>   {
>     ...
>   }
>
> ...although I personally am less convinced of the need. (OTOH, I think
> this would be a use case that is not currently possible, unlike the
> others which are mainly syntatic sugar.)


If you want to get this into the standard, you'll need to compare it with
what can be done with the existing language and library facilities, and
show that it's worth the additional cost of a language feature. For
instance, comparing it to each of the following would be a good start:

A core language alternative, if you don't need to jump/return out of the
scope:

// Example 1
std::lock_guard(mutex), [&] {
  synchronized_operation();
}();

With a tiny library, you can make this work:

// Example 2
with (gl::enabled(GL_BLEND)),
with (gl::enabled(GL_DEPTH_TEST)),
with (gl::bindShader(myShader)),
[&] {
  // ... code to render the scene...
}; // no parens needed here any more through operator, overload but still
need semicolon. 'return', 'break', 'continue', 'goto' still broken

If you're prepared to use macros, then you can make 'return' work too:

// Example 4
#define synchronized(lock) for (bool _done =3D false; !_done;) for (auto
&&_guard =3D std::lock_guard(lock); !_done; _done =3D true)

.... but 'break' and 'continue' are still broken. If you want to take the
macro route, you can get a solid solution by using an 'if', but you lose
some generality:

  struct LockGuard {
    std::lock_guard m;
    template<typename T> LockGuard(T &&t) : m(std::forward<T>(t)) {}
    operator bool() const { return false; }
  };
  #define synchronized(lock) if (LockGuard guard(lock)) {} else

.... and if you just want to extend the lifetime of a variable:

// Example 3
{
  auto fp =3D OpenFile(path);
  fp.write("Hello, world!");
  fp.write(additional_data);
}

//  Example 5
{
  std::unique_ptr<Foo> foo{nullptr};
  if (...)
    foo =3D std::make_unique<Foo>(...);
  ...
}

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

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

<div dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote">On F=
ri, Oct 3, 2014 at 3:22 PM, Matthew Woehlke <span dir=3D"ltr">&lt;<a href=
=3D"mailto:mw_triad@users.sourceforge.net" target=3D"_blank">mw_triad@users=
..sourceforge.net</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote"=
 style=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:=
rgb(204,204,204);border-left-style:solid;padding-left:1ex">The (somewhat no=
t so) recent thread on local variable scope reminded me<br>
of this again... it&#39;s come up a few times to have something like<br>
Python&#39;s &#39;with&#39; keyword.<br>
<br>
AFAIK that stalled out without getting anywhere.<br>
<br>
What do folks thing about:<br>
<br>
=C2=A0 using (=C2=ABstatement-list=C2=BB)<br>
=C2=A0 =C2=A0 =C2=ABstatement=C2=BB<br>
<br>
....which effectively expands to:<br>
<br>
=C2=A0 {<br>
=C2=A0 =C2=A0 PRESERVE(=C2=ABstatement-list=C2=BB)<br>
=C2=A0 =C2=A0 =C2=ABstatement=C2=BB<br>
=C2=A0 }<br>
<br>
....where &quot;PRESERVE&quot; is a &#39;magic function&#39; that delays des=
truction of any<br>
rvalues present in =C2=ABstatement-list=C2=BB until the end of the enclosin=
g<br>
scope. (I picked &quot;using&quot; instead of &quot;with&quot; because it a=
) seems<br>
reasonable and b) would avoid adding a new keyword, but I&#39;m not wedded<=
br>
to it.)<br>
<br>
=C2=ABstatement=C2=BB can be simple (&quot;foo();&quot;) or compound (&quot=
;{ foo(); bar(); }&quot;).<br>
=C2=ABstatement-list=C2=BB is defined as =C2=ABsimple-statement=C2=BB[; =C2=
=ABstatement-list=C2=BB]<br>
(IOW, &quot;auto a =3D foo()&quot;, &quot;foo(); bar()&quot;, etc.)<br>
<br>
This would also mitigate the desire for anonymous variables.<br>
<br>
<br>
<br>
Here&#39;s some &quot;concrete&quot; examples:<br>
<br>
=C2=A0 // Example 1<br>
=C2=A0 using (std::lock_guard(mutex))<br>
=C2=A0 =C2=A0 synchronized_operation();<br>
<br>
=C2=A0 // Example 2<br>
=C2=A0 using (gl::enabled(GL_BLEND);<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0gl::enabled(GL_DEPTH_TEST);<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0gl::bindShader(myShader))<br>
=C2=A0 {<br>
=C2=A0 =C2=A0 // ...code to render the scene...<br>
=C2=A0 }<br>
<br>
=C2=A0 // Example 3<br>
=C2=A0 using (auto fp =3D OpenFile(path))<br>
=C2=A0 {<br>
=C2=A0 =C2=A0 fp.write(&quot;Hello, world!&quot;);<br>
=C2=A0 =C2=A0 fp.write(additional_data);<br>
=C2=A0 }<br>
<br>
=C2=A0 // Example 4<br>
=C2=A0 #define synchronized(lock) using (std::lock_guard(lock))<br>
<br>
=C2=A0 // Example 5<br>
=C2=A0 using (std::unique_ptr&lt;Foo&gt; foo{nullptr})<br>
=C2=A0 {<br>
=C2=A0 =C2=A0 if (...)<br>
=C2=A0 =C2=A0 =C2=A0 foo =3D std::make_unique&lt;Foo&gt;(...);<br>
=C2=A0 =C2=A0 ...<br>
=C2=A0 }<br>
<br>
<br>
<br>
It was also requested to allow:<br>
<br>
=C2=A0 class Foo { ... };<br>
=C2=A0 Foo::Foo(...) using(std::lock_guard(...)<br>
=C2=A0 =C2=A0 : ...initializers...<br>
=C2=A0 {<br>
=C2=A0 =C2=A0 ...<br>
=C2=A0 }<br>
<br>
....although I personally am less convinced of the need. (OTOH, I think<br>
this would be a use case that is not currently possible, unlike the<br>
others which are mainly syntatic sugar.)</blockquote><div><br></div><div>If=
 you want to get this into the standard, you&#39;ll need to compare it with=
 what can be done with the existing language and library facilities, and sh=
ow that it&#39;s worth the additional cost of a language feature. For insta=
nce, comparing it to each of the following would be a good start:</div><div=
><br></div><div>A core language alternative, if you don&#39;t need to jump/=
return out of the scope:</div><div><br></div><div>// Example 1</div><div>st=
d::lock_guard(mutex), [&amp;] {</div><div>=C2=A0 synchronized_operation();<=
/div><div>}();</div><div><br></div><div>With a tiny library, you can make t=
his work:</div><div><br></div><div>// Example 2</div><div>with (gl::enabled=
(GL_BLEND)),</div><div>with (gl::enabled(GL_DEPTH_TEST)),</div><div>with (g=
l::bindShader(myShader)),</div><div>[&amp;] {</div><div>=C2=A0 // ... code =
to render the scene...</div><div>}; // no parens needed here any more throu=
gh operator, overload but still need semicolon. &#39;return&#39;, &#39;brea=
k&#39;, &#39;continue&#39;, &#39;goto&#39; still broken</div><div><br></div=
><div>If you&#39;re prepared to use macros, then you can make &#39;return&#=
39; work too:</div><div><br></div><div>// Example 4</div><div>#define synch=
ronized(lock) for (bool _done =3D false; !_done;) for (auto &amp;&amp;_guar=
d =3D std::lock_guard(lock); !_done; _done =3D true)</div><div><br></div><d=
iv>... but &#39;break&#39; and &#39;continue&#39; are still broken. If you =
want to take the macro route, you can get a solid solution by using an &#39=
;if&#39;, but you lose some generality:</div><div><br></div><div>=C2=A0 str=
uct LockGuard {</div><div>=C2=A0 =C2=A0 std::lock_guard m;</div><div>=C2=A0=
 =C2=A0 template&lt;typename T&gt; LockGuard(T &amp;&amp;t) : m(std::forwar=
d&lt;T&gt;(t)) {}</div><div>=C2=A0 =C2=A0 operator bool() const { return fa=
lse; }</div><div>=C2=A0 };</div><div>=C2=A0 #define synchronized(lock) if (=
LockGuard guard(lock)) {} else</div><div><br></div><div>... and if you just=
 want to extend the lifetime of a variable:</div><div><br></div><div>// Exa=
mple 3</div><div>{</div><div>=C2=A0 auto fp =3D OpenFile(path);</div><div>=
=C2=A0 fp.write(&quot;Hello, world!&quot;);</div><div>=C2=A0 fp.write(addit=
ional_data);</div><div>}</div><div><br></div><div>// =C2=A0Example 5</div><=
div>{</div><div>=C2=A0 std::unique_ptr&lt;Foo&gt; foo{nullptr};</div><div>=
=C2=A0 if (...)</div><div>=C2=A0 =C2=A0 foo =3D std::make_unique&lt;Foo&gt;=
(...);</div><div>=C2=A0 ...</div><div>}</div></div></div></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

--047d7b342d32ebb5b805048eb1b8--

.


Author: David Krauss <potswa@gmail.com>
Date: Sat, 4 Oct 2014 10:34:46 +0800
Raw View
--Apple-Mail=_E6CE9DAA-33DF-4AD4-9090-23479CB9E219
Content-Type: text/plain; charset=ISO-8859-1


On 2014-10-04, at 9:23 AM, Richard Smith <richard@metafoo.co.uk> wrote:

> A core language alternative, if you don't need to jump/return out of the scope:
>
> // Example 1
> std::lock_guard(mutex), [&] {
>   synchronized_operation();
> }();

Very clever!

I wonder, though, is there any (current, practical) overhead to using a lambda as a potentially sizable scope, with a large number of captured variables?

http://stackoverflow.com/q/24303079/153285

--

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

--Apple-Mail=_E6CE9DAA-33DF-4AD4-9090-23479CB9E219
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=ISO-8859-1

<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>On 2014&=
ndash;10&ndash;04, at 9:23 AM, Richard Smith &lt;<a href=3D"mailto:richard@=
metafoo.co.uk">richard@metafoo.co.uk</a>&gt; wrote:</div><br class=3D"Apple=
-interchange-newline"><blockquote type=3D"cite"><div style=3D"font-family: =
Helvetica; 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; text-transform: none; white-space: no=
rmal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><di=
v dir=3D"ltr"><div class=3D"gmail_extra"><div class=3D"gmail_quote"><div>A =
core language alternative, if you don't need to jump/return out of the scop=
e:</div><div><br></div><div>// Example 1</div><div>std::lock_guard(mutex), =
[&amp;] {</div><div>&nbsp; synchronized_operation();</div><div>}();</div></=
div></div></div></div></blockquote><div><br></div><div>Very clever!</div><d=
iv><br></div><div>I wonder, though, is there any (current, practical) overh=
ead to using a lambda as a potentially sizable scope, with a large number o=
f captured variables?</div><div><br></div><div><a href=3D"http://stackoverf=
low.com/q/24303079/153285">http://stackoverflow.com/q/24303079/153285</a></=
div><div><br></div></div></body></html>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

--Apple-Mail=_E6CE9DAA-33DF-4AD4-9090-23479CB9E219--

.


Author: Bo Pesson <bop@gmb.dk>
Date: Sat, 04 Oct 2014 21:55:41 +0200
Raw View
On 2014-10-04 00:22, Matthew Woehlke wrote:
> The (somewhat not so) recent thread on local variable scope reminded me
> of this again... it's come up a few times to have something like
> Python's 'with' keyword.
>
> AFAIK that stalled out without getting anywhere.
>
> What do folks thing about:
>
>    using (=C2=ABstatement-list=C2=BB)
>      =C2=ABstatement=C2=BB
>
> ...which effectively expands to:
>
>    {
>      PRESERVE(=C2=ABstatement-list=C2=BB)
>      =C2=ABstatement=C2=BB
>    }
>
> ...where "PRESERVE" is a 'magic function' that delays destruction of any
> rvalues present in =C2=ABstatement-list=C2=BB until the end of the enclos=
ing
> scope. (I picked "using" instead of "with" because it a) seems
> reasonable and b) would avoid adding a new keyword, but I'm not wedded
> to it.)
>
> =C2=ABstatement=C2=BB can be simple ("foo();") or compound ("{ foo(); bar=
(); }").
> =C2=ABstatement-list=C2=BB is defined as =C2=ABsimple-statement=C2=BB[; =
=C2=ABstatement-list=C2=BB]
> (IOW, "auto a =3D foo()", "foo(); bar()", etc.)
>
> This would also mitigate the desire for anonymous variables.
>
>
>
> Here's some "concrete" examples:
>
>    // Example 1
>    using (std::lock_guard(mutex))
>      synchronized_operation();
>

This alread works without any new syntax:

    {
       std::lock_guard<std::mutex>  guard(mutex);
       synchronized_operation();
    }


In general I'm not sure I would favor new syntax making it easier to=20
write more code inline rather that creating separate functions where the=20
temporaries can be either local variables or parameters.


Bo Persson


--=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: Matthew Woehlke <mw_triad@users.sourceforge.net>
Date: Mon, 06 Oct 2014 11:20:14 -0400
Raw View
On 2014-10-04 15:55, Bo Pesson wrote:
> On 2014-10-04 00:22, Matthew Woehlke wrote:
>> The (somewhat not so) recent thread on local variable scope reminded me
>> of this again... it's come up a few times to have something like
>> Python's 'with' keyword.
>>
>> AFAIK that stalled out without getting anywhere.
>>
>> What do folks thing about:
>>
>>    using (=C2=ABstatement-list=C2=BB)
>>      =C2=ABstatement=C2=BB
>>
>> [snip]
>=20
> This alread works without any new syntax:
>=20
>    {
>       std::lock_guard<std::mutex>  guard(mutex);
>       synchronized_operation();
>    }

Yes, but a) the bare brace syntax is ugly, and b) you still have to
provide a name for the temporary. The objective here is to address both
of those issues, especially the second one. Richard's suggestion OTOH
mostly addresses those, but at the cost of losing the ability to use
'break', 'return', etc. in the enclosing scope as in the parent scope
(by making the enclosing scope a lambda).

Yes, it's *generally* possible to achieve most of the examples
otherwise, but variable name eliding would requires macros at least, or
else use of a lambda is required.

> In general I'm not sure I would favor new syntax making it easier to
> write more code inline rather that creating separate functions where the
> temporaries can be either local variables or parameters.

Again, name eliding is the big difference...

--=20
Matthew

--=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: Matthew Woehlke <mw_triad@users.sourceforge.net>
Date: Tue, 07 Oct 2014 17:57:42 -0400
Raw View
On 2014-10-03 19:36, David Krauss wrote:
> On 2014-10-04, at 6:22 AM, Matthew Woehlke wrote:
>> ...where "PRESERVE" is a 'magic function' that delays destruction of any
>> rvalues present in <<statement-list>> until the end of the enclosing
>> scope. (I picked "using" instead of "with" because it a) seems
>> reasonable and b) would avoid adding a new keyword, but I'm not wedded
>> to it.)
>
> My generalized lifetime extension proposal allows for that, as long
> as the function is used to initialize a local variable. See the
> thread, posted a couple days ago.

So, I read through that finally (well, skimmed), but I'm not sure it
relates.

Here's a better way of phrasing it... the effect of:

  using(expr) { /*body*/ }

....shall be equivalent to:

  {
    auto&& __nameless__ = expr;
    /*body*/
  }

....where __nameless__ is in fact not a name as such, but merely
indicates that the result of 'expr' is lifetime-extended until the end
of the indicated scope. Said result is not accessible to the programmer.

It seems to me that my proposal relates to performing the lifetime
extension without binding the result to a name, whereas yours relates to
the implementation of lifetime extension in general and would apply
whether or not the result is bound to a name?

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

.