Allowing unnammed loop variable in range-based for loops?

217 views
Skip to first unread message

schreiber...@gmail.com

unread,
Jun 3, 2016, 6:37:16 AM6/3/16
to ISO C++ Standard - Future Proposals
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.

T. C.

unread,
Jun 3, 2016, 6:54:11 PM6/3/16
to ISO C++ Standard - Future Proposals, schreiber...@gmail.com
for( [[maybe_unused]] int i : range(10)) {

}
 

Jacob Lifshay

unread,
Jun 3, 2016, 11:23:16 PM6/3/16
to ISO C++ Standard - Future Proposals
You could also extend the syntax to
for( : range(25))
{
}

to not even dereference the implicit iterator.

Nevin Liber

unread,
Jun 3, 2016, 11:27:49 PM6/3/16
to std-pr...@isocpp.org
On 3 June 2016 at 05:37, <schreiber...@gmail.com> wrote:

Obviously there are various ways around this, in particular using the traditional for loop does not generate a warning.

Yes, and IMO it doesn't come up often enough to warrant a language change.
--
 Nevin ":-)" Liber  <mailto:ne...@eviloverlord.com>  +1-847-691-1404

Sam Kellett

unread,
Jun 4, 2016, 7:56:50 AM6/4/16
to std-pr...@isocpp.org
different styles i guess but it definitely comes up enough for me. and i think will for even more too when the range ts is accepted and the std:: equivalent of boost::irange is preferred to for(..; ..; ..).

i'd be okay with something like:

    for (std::ignore : range(10)) {
      // ...
    }

but really i think a wildcard symbol for unused variables (ala googletest, python, etc...) would be best general solution to this and much more.

Nicol Bolas

unread,
Jun 5, 2016, 10:38:42 AM6/5/16
to ISO C++ Standard - Future Proposals
On Saturday, June 4, 2016 at 7:56:50 AM UTC-4, Sam Kellett wrote:
On 4 June 2016 at 04:27, Nevin Liber <ne...@eviloverlord.com> wrote:
On 3 June 2016 at 05:37, <schreiber...@gmail.com> wrote:

Obviously there are various ways around this, in particular using the traditional for loop does not generate a warning.

Yes, and IMO it doesn't come up often enough to warrant a language change.
--
 Nevin ":-)" Liber  <mailto:ne...@eviloverlord.com>  +1-847-691-1404

different styles i guess but it definitely comes up enough for me. and i think will for even more too when the range ts is accepted and the std:: equivalent of boost::irange is preferred to for(..; ..; ..).

I have written, I'm going to go with thousands of `for` loops. Pre-range for loops.

I cannot recall a single time where I didn't use the loop counter somewhere in the body. So I don't see this suddenly becoming very popular with ranges.

BTW, the current version of the Range TS doesn't include a range for anything like this. It's mainly focused on concepts and algorithms. The next version should include more range types, adapters, and so forth.
 
but really i think a wildcard symbol for unused variables (ala googletest, python, etc...) would be best general solution to this and much more.

Now that, we can talk about. There are plenty of times where it is decidedly useful to create a variable and never access it. This would be for cases when you want the destructor to do some cleanup work. `scope_exit` and such things. In those cases, there is no need to name the variable.

Permitting such nameless variables in a range-for would be appropriate.

Viacheslav Usov

unread,
Jun 6, 2016, 6:03:19 AM6/6/16
to ISO C++ Standard - Future Proposals
On Sun, Jun 5, 2016 at 4:38 PM, Nicol Bolas <jmck...@gmail.com> wrote:

> There are plenty of times where it is decidedly useful to create a variable and never access it. This would be for cases when you want the destructor to do some cleanup work. `scope_exit` and such things. In those cases, there is no need to name the variable.

There was a thread a while ago (or was it this same thread?) that also started with nameless loop variables and ended up with, or made a major detour into, nameless scope guards. I'm not sure what the conclusion was. I think I said it then, and I will repeat again, that I generally concur with your assessment: nameless loop variables are not that useful, while nameless scope guards would be very nice to have; if they can double as nameless loop variables, well, OK. Doing it otherwise, i.e., having nameless loop vars but no nameless scope guards would only be adding baroqueness to the language.

Cheers,
V.

Sam Kellett

unread,
Jun 6, 2016, 6:19:21 AM6/6/16
to std-pr...@isocpp.org
On 5 June 2016 at 15:38, Nicol Bolas <jmck...@gmail.com> wrote:
On Saturday, June 4, 2016 at 7:56:50 AM UTC-4, Sam Kellett wrote:
On 4 June 2016 at 04:27, Nevin Liber <ne...@eviloverlord.com> wrote:
On 3 June 2016 at 05:37, <schreiber...@gmail.com> wrote:

Obviously there are various ways around this, in particular using the traditional for loop does not generate a warning.

Yes, and IMO it doesn't come up often enough to warrant a language change.
--
 Nevin ":-)" Liber  <mailto:ne...@eviloverlord.com>  +1-847-691-1404

different styles i guess but it definitely comes up enough for me. and i think will for even more too when the range ts is accepted and the std:: equivalent of boost::irange is preferred to for(..; ..; ..).

I have written, I'm going to go with thousands of `for` loops. Pre-range for loops.

I cannot recall a single time where I didn't use the loop counter somewhere in the body. So I don't see this suddenly becoming very popular with ranges.

do you make a point of remembering details about every for loop you write or are you assuming that nothing distinct in memory means never done? if the former: how many times have you used the variable once? how about twice?

schreiber...@gmail.com

unread,
Jun 8, 2016, 3:09:23 PM6/8/16
to ISO C++ Standard - Future Proposals
Le lundi 6 juin 2016 12:03:19 UTC+2, Viacheslav Usov a écrit :
There was a thread a while ago (or was it this same thread?) that also started with nameless loop variables and ended up with, or made a major detour into, nameless scope guards. I'm not sure what the conclusion was. I think I said it then, and I will repeat again, that I generally concur with your assessment: nameless loop variables are not that useful, while nameless scope guards would be very nice to have; if they can double as nameless loop variables, well, OK. Doing it otherwise, i.e., having nameless loop vars but no nameless scope guards would only be adding baroqueness to the language.

Right, you are probably referring to this thread:
https://groups.google.com/a/isocpp.org/forum/#!searchin/std-proposals/loop/std-proposals/ioi76TkG6g4/uwLnH266AQAJ
Sorry I failed to find it before... I agree with your conclusion and Nicol's, I was just reporting a single use case, but a generic solution for unnamed variables would be very more useful.
Reply all
Reply to author
Forward
0 new messages