220 6988 <CAOfiQqnr1C=78ARnZEvdNn=Pevz6nW1BhBz8tcFNcf9phJWEVg@mail.gmail.com> article
Path: news.gmane.org!not-for-mail
From: Richard Smith <richard@metafoo.co.uk>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Unnamed LValues
Date: Fri, 4 Oct 2013 18:33:43 -0700
Lines: 213
Approved: news@gmane.org
Message-ID: <CAOfiQqnr1C=78ARnZEvdNn=Pevz6nW1BhBz8tcFNcf9phJWEVg@mail.gmail.com>
References: <CADbh+eSOH540anYnezFkm4_wK+MCtMh-01XNzAcMQMebdCPgRQ@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=047d7bf0f67ad78a0804e7f468d5
X-Trace: ger.gmane.org 1380936824 31254 80.91.229.3 (5 Oct 2013 01:33:44 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Sat, 5 Oct 2013 01:33:44 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDVNBJG4YAIBB6GYXWJAKGQEOCGPGAI@isocpp.org Sat Oct 05 03:33:48 2013
Return-path: <std-proposals+bncBDVNBJG4YAIBB6GYXWJAKGQEOCGPGAI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-qa0-f72.google.com ([209.85.216.72])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBDVNBJG4YAIBB6GYXWJAKGQEOCGPGAI@isocpp.org>)
	id 1VSGkb-0002BD-EW
	for gclcip-std-proposals@m.gmane.org; Sat, 05 Oct 2013 03:33:45 +0200
Original-Received: by mail-qa0-f72.google.com with SMTP id j7sf4098293qaq.11
        for <gclcip-std-proposals@m.gmane.org>; Fri, 04 Oct 2013 18:33:44 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=mime-version:sender:in-reply-to:references:date:message-id:subject
         :from:to:x-original-sender:x-original-authentication-results
         :reply-to:precedence:mailing-list:list-id:list-post:list-help
         :list-archive:list-subscribe:list-unsubscribe:content-type;
        bh=zirIVQ8i8mnEDMthO8sdcjhuNvwwVtX816LXjPRgf1Q=;
        b=WBGJ4wYi1UH0Hrq1Ao8cB38LuGwsun5EGo7rTWC1DQvJVR1FhLJZiQwTJZgXU4kznr
         GF+xnJKfNZqT4G/FutQXdsqcX2goO3zRdyR8QY/F41iIbNB4IGgjz8gGYROpdBKfAnwO
         L8gmzSXE3wfo4kui8XYrSCpyQgJ/8WqU0ySg+K74feMG6ezt8zx1G4rdo89FS6XNEa9b
         1Wpz6qzRrq2bLiOvVcA8R0+BlhsYKw6ykbRULhFyZKsYFtFdT6IOfr5AsmV7EETqhJCo
         VsUFUz3AxmX4kB/lfVuCUkVgsslFC8xePH62aAu3fmEhccV4+ttk9NS6vdoMSR25w/30
         dm+w==
X-Received: by 10.236.56.70 with SMTP id l46mr17510319yhc.2.1380936824599;
        Fri, 04 Oct 2013 18:33:44 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.49.104.84 with SMTP id gc20ls1565478qeb.96.gmail; Fri, 04 Oct
 2013 18:33:44 -0700 (PDT)
X-Received: by 10.220.145.132 with SMTP id d4mr14759221vcv.9.1380936823962;
        Fri, 04 Oct 2013 18:33:43 -0700 (PDT)
Original-Received: from mail-vb0-x22c.google.com (mail-vb0-x22c.google.com [2607:f8b0:400c:c02::22c])
        by mx.google.com with ESMTPS id nr7si4191602vec.27.1969.12.31.16.00.00
        (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
        Fri, 04 Oct 2013 18:33:43 -0700 (PDT)
Received-SPF: pass (google.com: domain of metafoo@gmail.com designates 2607:f8b0:400c:c02::22c as permitted sender) client-ip=2607:f8b0:400c:c02::22c;
Original-Received: by mail-vb0-f44.google.com with SMTP id e13so2608533vbg.17
        for <std-proposals@isocpp.org>; Fri, 04 Oct 2013 18:33:43 -0700 (PDT)
X-Received: by 10.220.181.136 with SMTP id by8mr15047029vcb.11.1380936823833;
 Fri, 04 Oct 2013 18:33:43 -0700 (PDT)
Original-Sender: metafoo@gmail.com
Original-Received: by 10.220.244.137 with HTTP; Fri, 4 Oct 2013 18:33:43 -0700 (PDT)
In-Reply-To: <CADbh+eSOH540anYnezFkm4_wK+MCtMh-01XNzAcMQMebdCPgRQ@mail.gmail.com>
X-Original-Sender: richard@metafoo.co.uk
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of metafoo@gmail.com designates 2607:f8b0:400c:c02::22c as permitted
 sender) smtp.mail=metafoo@gmail.com;       dkim=pass header.i=@gmail.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:6988
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/6988>

--047d7bf0f67ad78a0804e7f468d5
Content-Type: text/plain; charset=ISO-8859-1

On Fri, Oct 4, 2013 at 6:01 PM, Brent Friedman <fourthgeek@gmail.com> wrote:

> When using RAII, the need to give the variable an explicit name is
> cumbersome and often not useful. I'll use the example of freezing and
> thawing in WxWidgets, but the problem is applicable to most any RAII
> scenario.
>
> class WxScopeFreeze; //Freeze() in constructor, Thaw() in destructor
>
> WxScopeFreeze Freeze(this);
>
> Later on, I want to freeze an additional widget. I should probably update
> and maintain these variable names now.
>
> WxScopeFreeze FreezeThis(this);
> WxScopeFreeze FreezeOther(Other);
>
> Unfortunately, omitting the name is no good here. Without a name, the
> object lifetime ends at the end of the full expression and we don't get our
> scope freezing behavior.
>
> WxScopeFreeze(this); //doesn't live for the full scope
>
> If your object is generated as the result of a function call (esp. for
> template argument deduction) then the syntax grows yet still.
>
> auto&& FreezeThis = ScopeFreeze(this);
> auto&& FreezeOther = ScopeFreeze(Other);
>
>
> Let's consider ways to get around this issue. The obvious one would be a
> macro.
> #define SCOPE_FREEZE(expr) WxScopeFreeze Freeze##__LINE__##(expr);
>
> This hides the noise to some extent; you no longer have to think about
> what to name this RAII object and there won't be any name clashes as long
> as you don't declare multiples on the same line of code. The __COUNTER__
> extension supported by some compilers would give you some alternative.
> __COUNTER__ would be quite unsafe to use in the name of a variable which
> you declare in a header though.
>
> There are more downsides to the macro approach.
> * It requires the existence of the macro for every RAII type helper you
> want.
> * For our safer __LINE__ solution, names can still clash if they were
> defined in a separate file (eg, header). These may not generate compilation
> errors, but could generate spurious static analysis warnings and confusion
> when debugging.
> * In a debugger window, you'll see variable names that you never created
> and that may be difficult to refer back to.
> * Its a macro
>
> Instead of macro invention, how about adding some syntax for unnamed
> variables?
>
> You could repurpose auto when used as the name of a variable.
> WxScopeFreeze auto(this);
> WxScopeFreeze auto(Other);
> auto auto{WxScopeFreeze(this)};
>
> In that last example we use type deduction on an unnamed variable. It
> would be nice to standardize that idea a bit more, but I'm not sure what
> the syntax should be.
>
>
> We could consider some other keyword to identify unnamed variables.
> Unfortunately, the obvious syntax of providing no name is taken as I
> mentioned.
>
> = is almost appealing, but wouldn't work well with lambdas. Ideally we can
> bind logic to the lifetime of a lambda with unnamed variable syntax.
> WxScopeFreeze =(this);
> WxScopeFreeze ={Other};
> [ ==WxScopeFreeze(this) ](){}; //weird?
>

The important part in such a construct is the point at which the RAII
object is destroyed. Perhaps the syntax should make that more explicit. How
about:

  WxScopeFreeze(this) {
    // ...
  } // dtor invoked here.

.... or ...

  /*template<typename Mutex> std::lock_guard<Mutex> locked(Mutex &);*/

  locked(my_mutex) {
  }

You can get that effect currently with:

  WxScopeFreeze(this), [&]{
    // ...
  }();

.... although this use of the comma operator is unfamiliar to most, and this
doesn't support control flow leaving the block.

-- 

--- 
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/.

--047d7bf0f67ad78a0804e7f468d5
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Fri, Oct 4, 2013 at 6:01 PM, Brent Friedman <span dir=
=3D"ltr">&lt;<a href=3D"mailto:fourthgeek@gmail.com" target=3D"_blank">four=
thgeek@gmail.com</a>&gt;</span> wrote:<br><div class=3D"gmail_extra"><div c=
lass=3D"gmail_quote">
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div dir=3D"ltr">When using RAII, the need t=
o give the variable an explicit name is cumbersome and often not useful. I&=
#39;ll use the example of freezing and thawing in WxWidgets, but the proble=
m is applicable to most any RAII scenario.<div>

<br></div><div>class WxScopeFreeze; //Freeze() in constructor, Thaw() in de=
structor=A0</div><div><br></div><div>WxScopeFreeze Freeze(this);</div><div>=
<br></div><div>Later on, I want to freeze an additional widget. I should pr=
obably update and maintain these variable names now.</div>

<div><br></div><div>WxScopeFreeze FreezeThis(this);</div><div>WxScopeFreeze=
 FreezeOther(Other);</div><div><br></div><div>Unfortunately, omitting the n=
ame is no good here. Without a name, the object lifetime ends at the end of=
 the full expression and we don&#39;t get our scope freezing behavior.</div=
>

<div><br></div><div>WxScopeFreeze(this); //doesn&#39;t live for the full sc=
ope</div><div><br></div><div>If your object is generated as the result of a=
 function call (esp. for template argument deduction) then the syntax grows=
 yet still.</div>

<div><br></div><div>auto&amp;&amp; FreezeThis =3D ScopeFreeze(this);</div><=
div>auto&amp;&amp; FreezeOther =3D ScopeFreeze(Other);</div><div><br></div>=
<div><br></div><div>Let&#39;s consider ways to get around this issue. The o=
bvious one would be a macro.</div>

<div>#define SCOPE_FREEZE(expr) WxScopeFreeze Freeze##__LINE__##(expr);</di=
v><div><br></div><div>This hides the noise to some extent; you no longer ha=
ve to think about what to name this RAII object and there won&#39;t be any =
name clashes as long as you don&#39;t declare multiples on the same line of=
 code. The __COUNTER__ extension supported by some compilers would give you=
 some alternative. __COUNTER__ would be quite unsafe to use in the name of =
a variable which you declare in a header though.</div>

<div><br></div><div>There are more downsides to the macro approach.=A0</div=
><div><div>* It requires the existence of the macro for every RAII type hel=
per you want.</div><div>* For our safer __LINE__ solution, names can still =
clash if they were defined in a separate file (eg, header). These may not g=
enerate compilation errors, but could generate spurious static analysis war=
nings and confusion when debugging.</div>

</div><div>* In a debugger window, you&#39;ll see variable names that you n=
ever created and that may be difficult to refer back to.</div><div>* Its a =
macro</div><div><br></div><div>Instead of macro invention, how about adding=
 some syntax for unnamed variables?</div>

<div><br></div><div>You could repurpose auto when used as the name of a var=
iable.</div><div>WxScopeFreeze auto(this);</div><div>WxScopeFreeze auto(Oth=
er);</div><div>auto auto{WxScopeFreeze(this)};</div><div><br></div><div>

In that last example we use type deduction on an unnamed variable. It would=
 be nice to standardize that idea a bit more, but I&#39;m not sure what the=
 syntax should be.</div><div><br></div><div><br></div><div>We could conside=
r some other keyword to identify unnamed variables. Unfortunately, the obvi=
ous syntax of providing no name is taken as I mentioned.</div>

<div><br></div><div>=3D is almost appealing, but wouldn&#39;t work well wit=
h lambdas. Ideally we can bind logic to the lifetime of a lambda with unnam=
ed variable syntax.</div><div>WxScopeFreeze =3D(this);</div><div>WxScopeFre=
eze =3D{Other};</div>

<div>[ =3D=3DWxScopeFreeze(this) ](){}; //weird?</div></div></blockquote><d=
iv><br></div><div>The important part in such a construct is the point at wh=
ich the RAII object is destroyed. Perhaps the syntax should make that more =
explicit. How about:</div>
<div><br></div><div>=A0 WxScopeFreeze(this) {</div><div>=A0 =A0 // ...</div=
><div>=A0 } // dtor invoked here.</div><div><br></div><div>... or ...</div>=
<div><br></div><div>=A0 /*template&lt;typename Mutex&gt; std::lock_guard&lt=
;Mutex&gt; locked(Mutex &amp;);*/</div>
<div><br></div><div>=A0 locked(my_mutex) {</div><div>=A0 }</div><div><br></=
div><div>You can get that effect currently with:</div><div><br></div><div>=
=A0 WxScopeFreeze(this), [&amp;]{</div><div>=A0 =A0 // ...</div><div>=A0 }(=
);</div><div>
<br></div><div>... although this use of the comma operator is unfamiliar to=
 most, and this doesn&#39;t support control flow leaving the block.</div></=
div></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 />

--047d7bf0f67ad78a0804e7f468d5--

.
