Topic: Allowing unnammed loop variable in range-based for loops?


Author: schreiber.corentin@gmail.com
Date: Fri, 3 Jun 2016 03:37:16 -0700 (PDT)
Raw View
------=_Part_204_1560168322.1464950236858
Content-Type: multipart/alternative;
 boundary="----=_Part_205_291784764.1464950236859"

------=_Part_205_291784764.1464950236859
Content-Type: text/plain; charset=UTF-8

I have defined a utility function "range(n)" that generates a input
sequence of integers from 0 to n-1 such that
for (int i : range(10)) {
    // ...
}

// Is equivalent to

for (int i = 0; i < 10; ++i) {
    // ...
}

I think a similar utility is present in the range TS, although I do not
know it exact name.

I recently encountered a situation when I needed to perform "n" iterations,
but without caring about "which" iteration is currently being performed; I
didn't use the value of "i" in the example above. The problem is, compilers
start warning on unused variable... See running code here:
http://cpp.sh/9lot

Obviously there are various ways around this, in particular using the
traditional for loop does not generate a warning. But range-based for loops
are superior in many ways, and it would be good to be able to use it in
this situation. I also think the warning is correct, since there is no way
for the compiler to know your intention here (maybe you just made a typo
and intended to use "i"?). Therefore, do you think it would be possible to
solve this issue by allowing the range variable to be unnamed? Something
like:

for (int : range(10)) {
    // ...
}

That would also make one's intention clearer.

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/6b4ef651-55b3-4b93-a6ac-574b55c7f759%40isocpp.org.

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

<div dir=3D"ltr">I have defined a utility function &quot;range(n)&quot; tha=
t generates a input sequence of integers from 0 to n-1 such that<br><div cl=
ass=3D"prettyprint" style=3D"background-color: rgb(250, 250, 250); border-c=
olor: rgb(187, 187, 187); border-style: solid; border-width: 1px; word-wrap=
: break-word;"><code class=3D"prettyprint"><div class=3D"subprettyprint"><s=
pan style=3D"color: #008;" class=3D"styled-by-prettify">for</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">(</span><span style=3D"color: #008;=
" class=3D"styled-by-prettify">int</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> i </span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">:</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> range</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">(</span><span style=3D"color: #066;" class=3D"styled-by-prettify">10</s=
pan><span style=3D"color: #660;" class=3D"styled-by-prettify">))</span><spa=
n 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">// ...</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;" c=
lass=3D"styled-by-prettify"><br><br></span><span style=3D"color: #800;" cla=
ss=3D"styled-by-prettify">// Is equivalent to</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"><br><br></span><span style=3D"color: #00=
8;" class=3D"styled-by-prettify">for</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">(</span><span style=3D"color: #008;" class=3D"styled-by-pr=
ettify">int</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> i </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span=
 style=3D"color: #066;" class=3D"styled-by-prettify">0</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"> i </span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">&lt;</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #066;" class=3D"style=
d-by-prettify">10</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">++</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify">i</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>=C2=A0 =C2=A0 </span><span style=3D"color: #800;" c=
lass=3D"styled-by-prettify">// ...</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">}</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"><br></span></div></code></div><br>I think a similar utility is pre=
sent in the range TS, although I do not know it exact name.<br><br>I recent=
ly encountered a situation when I needed to perform &quot;n&quot; iteration=
s, but without caring about &quot;which&quot; iteration is currently being =
performed; I didn&#39;t use the value of &quot;i&quot; in the example above=
.. The problem is, compilers start warning on unused variable... See running=
 code here: http://cpp.sh/9lot<br><br>Obviously there are various ways arou=
nd this, in particular using the traditional for loop does not generate a w=
arning. But range-based for loops are superior in many ways, and it would b=
e good to be able to use it in this situation. I also think the warning is =
correct, since there is no way for the compiler to know your intention here=
 (maybe you just made a typo and intended to use &quot;i&quot;?). Therefore=
, do you think it would be possible to solve this issue by allowing the ran=
ge variable to be unnamed? Something like:<br><br><div class=3D"prettyprint=
" style=3D"background-color: rgb(250, 250, 250); border-color: rgb(187, 187=
, 187); border-style: solid; border-width: 1px; word-wrap: break-word;"><co=
de class=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"color=
: #008;" class=3D"styled-by-prettify">for</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: #008;" class=3D"style=
d-by-prettify">int</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prettify">:=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> range</sp=
an><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span =
style=3D"color: #066;" class=3D"styled-by-prettify">10</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;" cl=
ass=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"><br>=C2=A0 =C2=A0 </span><span style=3D"color: #800;" cla=
ss=3D"styled-by-prettify">// ...</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">}</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"><br></span></div></code></div><br>That would also make one&#39;s i=
ntention clearer.<br></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/6b4ef651-55b3-4b93-a6ac-574b55c7f759%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/6b4ef651-55b3-4b93-a6ac-574b55c7f759=
%40isocpp.org</a>.<br />

------=_Part_205_291784764.1464950236859--

------=_Part_204_1560168322.1464950236858--

.


Author: Jacob Lifshay <programmerjake@gmail.com>
Date: Fri, 3 Jun 2016 20:23:15 -0700 (PDT)
Raw View
------=_Part_1270_1221469094.1465010596040
Content-Type: text/plain; charset=UTF-8

You could also extend the syntax to
for( : range(25))
{
}

to not even dereference the implicit iterator.

--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/b0a14f0f-a350-4a50-9c91-c3effed2ba1b%40isocpp.org.

------=_Part_1270_1221469094.1465010596040--

.