220 14494 <CADbh+eScmZMj4dcQv73re1dM0VNbw_PeHRc_FPWNONoKNKsiMg@mail.gmail.com> article
Path: news.gmane.org!not-for-mail
From: Brent Friedman <fourthgeek@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Re: Range-based if
Date: Tue, 11 Nov 2014 20:39:32 -0600
Lines: 212
Approved: news@gmane.org
Message-ID: <CADbh+eScmZMj4dcQv73re1dM0VNbw_PeHRc_FPWNONoKNKsiMg@mail.gmail.com>
References: <CADbh+eQCaTPArs7YBUg9mYwrnJ2Gu3SRAfqUbPiLscpyxV8JqQ@mail.gmail.com>
	<m3tvcu$9ia$1@ger.gmane.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=001a1130c512468fb00507a04e91
X-Trace: ger.gmane.org 1415759983 29159 80.91.229.3 (12 Nov 2014 02:39:43 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Wed, 12 Nov 2014 02:39:43 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBCHLPXGUXMJBBZMQRORQKGQEX42C3LA@isocpp.org Wed Nov 12 03:39:36 2014
Return-path: <std-proposals+bncBCHLPXGUXMJBBZMQRORQKGQEX42C3LA@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-pa0-f69.google.com ([209.85.220.69])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBCHLPXGUXMJBBZMQRORQKGQEX42C3LA@isocpp.org>)
	id 1XoNqI-0004AA-PQ
	for gclcip-std-proposals@m.gmane.org; Wed, 12 Nov 2014 03:39:35 +0100
Original-Received: by mail-pa0-f69.google.com with SMTP id eu11sf69945731pac.4
        for <gclcip-std-proposals@m.gmane.org>; Tue, 11 Nov 2014 18:39:34 -0800 (PST)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:mime-version: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=nOYdCeelEX84X4K+5VSjBsDNx8VnMDPz1zdimcWrn+k=;
        b=OU3t7U7iZinS3PHn7fLnVA1x3kbzVkKUq/LDzu5LiyNLuuNxGi0vKnXTWOwOQZeGNg
         snX47y99gg298QqsU8nhXRu3JnGzbdt4Zq25CZYjx0hM+NCT6G3hovGusenNBMEgrgZ4
         +nlxOMT5aQrjQzPEhDjdqN0uMZOTHVqWV2sMFxkt8OpDb7ZDnF3qGlr+Q1jSnJsWSF0H
         fuiL9LjdJTlWepx5+vR4C7pEh0BxxUGE0l0IUtLhoXV8n8gLb3eyhTpOzshIiG79i008
         FsuS7J1K/EkNbufgoHdlfRQ80zrQHP9CHMsSEU9ownnPwCjcZcxpZzAZEAFeaqNblzZo
         zVjA==
X-Gm-Message-State: ALoCoQmbHRDV784Y3o5YFpUKJNVwavPlXxrMf99lZti3GPu3Chx/KtL34JIcn5iZlneQ9pJEQp3I
X-Received: by 10.66.250.165 with SMTP id zd5mr33838517pac.27.1415759974024;
        Tue, 11 Nov 2014 18:39:34 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.182.245.131 with SMTP id xo3ls956153obc.50.gmail; Tue, 11 Nov
 2014 18:39:33 -0800 (PST)
X-Received: by 10.182.125.3 with SMTP id mm3mr35168927obb.7.1415759973171;
        Tue, 11 Nov 2014 18:39:33 -0800 (PST)
Original-Received: from mail-ob0-x22a.google.com (mail-ob0-x22a.google.com. [2607:f8b0:4003:c01::22a])
        by mx.google.com with ESMTPS id cr8si25511311oec.91.2014.11.11.18.39.33
        for <std-proposals@isocpp.org>
        (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
        Tue, 11 Nov 2014 18:39:33 -0800 (PST)
Received-SPF: pass (google.com: domain of fourthgeek@gmail.com designates 2607:f8b0:4003:c01::22a as permitted sender) client-ip=2607:f8b0:4003:c01::22a;
Original-Received: by mail-ob0-f170.google.com with SMTP id nt9so9169623obb.15
        for <std-proposals@isocpp.org>; Tue, 11 Nov 2014 18:39:33 -0800 (PST)
X-Received: by 10.60.245.99 with SMTP id xn3mr36467640oec.15.1415759972960;
 Tue, 11 Nov 2014 18:39:32 -0800 (PST)
Original-Received: by 10.202.232.77 with HTTP; Tue, 11 Nov 2014 18:39:32 -0800 (PST)
In-Reply-To: <m3tvcu$9ia$1@ger.gmane.org>
X-Original-Sender: fourthgeek@gmail.com
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of fourthgeek@gmail.com designates 2607:f8b0:4003:c01::22a as
 permitted sender) smtp.mail=fourthgeek@gmail.com;       dkim=pass
 header.i=@gmail.com;       dmarc=pass (p=NONE dis=NONE) header.from=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: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:14494
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/14494>

--001a1130c512468fb00507a04e91
Content-Type: text/plain; charset=UTF-8

> auto first_or_null(auto range) -> decltype(&range.begin())

This assumes pointers, which while addressing some use cases it isn't
sufficient for the general case.

>    if (!range.empty())
>        DoSomething(*range.begin());

This is more or less what we're doing currently. If you need to do more
than one something, then it becomes

if (!range.empty())
{
    auto&& front = *range.begin();
    DoSomething( front );
    //etc
}

'range' may of course be an rvalue, perhaps the result of a function call.
We do this a lot. Currently, we'll have to create a variable to store
'range'. So introducing an additional scope would be wise to contain
'range'.

{
  auto& range = GetTheRange();
  if (!range.empty())
  {
      auto& front = *range.begin();
      DoSomething( front );
      //etc
  }
}

In the face of this worst-case alternative, I think it's only reasonable
that someone would prefer the range-for-with-a-break.

Anecdotally, in our codebase of a million or so LOC, this pattern of using
a looping structure to get the first element in a range constituted about
70% of our unreachable code instances.


On Tue, Nov 11, 2014 at 3:34 PM, Matthew Woehlke <
mw_triad@users.sourceforge.net> wrote:

> On 2014-11-11 16:12, Brent Friedman wrote:
> > I've noted in our codebase that we lean heavily on the convenience of
> > range-based for even when it's not perhaps the best way to express an
> idea.
> >
> > //do something to the first item
> > for (auto& Item : range)
> > {
> >     DoSomething(Item);
> >     break;
> > }
>
>     auto first_or_null(auto range) -> decltype(&range.begin())
>     {
>         auto begin = range.begin();
>         auto end = range.end();
>         return (begin == end ? nullptr : &(*begin));
>     }
>
>     if (auto item = first_or_null(range))
>
> Basically you want that, right? :-)
>
> I'm not sure if that actually works, and it requires an additional
> dereference when you go to use 'item', but OTOH it wouldn't require a
> language change...
>
> That said, how often can you not write:
>
>     if (!range.empty())
>         DoSomething(*range.begin());
>
> ...?
>
> --
> 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/.
>

-- 

--- 
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/.

--001a1130c512468fb00507a04e91
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">&gt;<span style=3D"font-family:arial,sans-serif;font-size:=
13px">=C2=A0auto first_or_null(auto range) -&gt; decltype(&amp;range.begin(=
))</span><div><br></div><div>This assumes pointers, which while addressing =
some use cases it isn&#39;t sufficient for the general case.</div><div><br>=
</div><div>&gt;<span style=3D"font-family:arial,sans-serif;font-size:13px">=
=C2=A0 =C2=A0 if (!range.empty())</span></div><span style=3D"font-family:ar=
ial,sans-serif;font-size:13px">&gt; =C2=A0 =C2=A0 =C2=A0 =C2=A0DoSomething(=
*range.begin());</span><div><br></div><div>This is more or less what we&#39=
;re doing currently. If you need to do more than one something, then it bec=
omes</div><div><br></div><div>if (!range.empty())</div><div>{</div><div>=C2=
=A0 =C2=A0 auto&amp;&amp; front =3D *range.begin();</div><div>=C2=A0 =C2=A0=
 DoSomething( front );</div><div>=C2=A0 =C2=A0 //etc</div><div>}</div><div>=
<br></div><div>&#39;range&#39; may of course be an rvalue, perhaps the resu=
lt of a function call. We do this a lot. Currently, we&#39;ll have to creat=
e a variable to store &#39;range&#39;. So introducing an additional scope w=
ould be wise to contain &#39;range&#39;.</div><div><br></div><div>{</div><d=
iv>=C2=A0 auto&amp; range =3D GetTheRange();</div><div>=C2=A0 if (!range.em=
pty())</div><div>=C2=A0 {</div><div>=C2=A0 =C2=A0 =C2=A0 auto&amp; front =
=3D *range.begin();</div><div>=C2=A0 =C2=A0 =C2=A0 DoSomething( front );</d=
iv><div>=C2=A0 =C2=A0 =C2=A0 //etc</div><div>=C2=A0 }</div><div>}</div><div=
><br></div><div>In the face of this worst-case alternative, I think it&#39;=
s only reasonable that someone would prefer the range-for-with-a-break.</di=
v><div><br></div><div>Anecdotally, in our codebase of a million or so LOC, =
this pattern of using a looping structure to get the first element in a ran=
ge constituted about 70% of our unreachable code instances.</div><div>=C2=
=A0</div></div><div class=3D"gmail_extra"><br><div class=3D"gmail_quote">On=
 Tue, Nov 11, 2014 at 3:34 PM, Matthew Woehlke <span dir=3D"ltr">&lt;<a hre=
f=3D"mailto:mw_triad@users.sourceforge.net" target=3D"_blank">mw_triad@user=
s.sourceforge.net</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote=
" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><=
span class=3D"">On 2014-11-11 16:12, Brent Friedman wrote:<br>
&gt; I&#39;ve noted in our codebase that we lean heavily on the convenience=
 of<br>
&gt; range-based for even when it&#39;s not perhaps the best way to express=
 an idea.<br>
&gt;<br>
&gt; //do something to the first item<br>
&gt; for (auto&amp; Item : range)<br>
&gt; {<br>
&gt;=C2=A0 =C2=A0 =C2=A0DoSomething(Item);<br>
&gt;=C2=A0 =C2=A0 =C2=A0break;<br>
&gt; }<br>
<br>
</span>=C2=A0 =C2=A0 auto first_or_null(auto range) -&gt; decltype(&amp;ran=
ge.begin())<br>
=C2=A0 =C2=A0 {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 auto begin =3D range.begin();<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 auto end =3D range.end();<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return (begin =3D=3D end ? nullptr : &amp;(*beg=
in));<br>
=C2=A0 =C2=A0 }<br>
<br>
=C2=A0 =C2=A0 if (auto item =3D first_or_null(range))<br>
<br>
Basically you want that, right? :-)<br>
<br>
I&#39;m not sure if that actually works, and it requires an additional<br>
dereference when you go to use &#39;item&#39;, but OTOH it wouldn&#39;t req=
uire a<br>
language change...<br>
<br>
That said, how often can you not write:<br>
<br>
=C2=A0 =C2=A0 if (!range.empty())<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 DoSomething(*range.begin());<br>
<br>
....?<br>
<span class=3D"HOEnZb"><font color=3D"#888888"><br>
--<br>
Matthew<br>
<br>
--<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%2Bunsubscribe@isocpp.org">std-propo=
sals+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/" target=3D"_blank">http://groups.google.com/a/isocpp.org/gro=
up/std-proposals/</a>.<br>
</font></span></blockquote></div><br></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 />

--001a1130c512468fb00507a04e91--

.
