On 3 Nov., 19:42, Joe Gottman <jgott...@carolina.rr.com> wrote:
But this code would not be legal:
vector<int> vec;
int x = 0; // Declare x outside of loop
for ( x : vec) { /* do whatever */}
I agree that both the N2778 paper and the current draft
N2798 do exclude your second type. I do not know whether
this outcome was intended or not, but I tend to say that I
have no convincing arguments in my pocket to justify
such an extension. This kind of loop has no loop-counter
in the classical meaning, so the only advantage I can
think of is to have the very last logical element referenced
(I mean logical in sense, that this is not necessarily the
last sequence element, but possibly one earlier due to
possible usage of jumps). If I want to simulate this, I
could either use the classical for-statement or I use a
helper variable. The last one can be cheap, if I can choose
a reference-type as type-specifier of the for-range-declaration,
e.g. in
vector<int> vec;
int x = 0; // Declare x outside of loop
for (const int& x2 : vec) {
x = x2;
/* do whatever */
}
I have used such for-each-style loops in Delphi, Java, and
C# before and never missed this reduced functionality.
looks like too ugly a hack to me.
every misuse." but I don't even see which misuse could possibly happen.
[ comp.lang.c++.moderated. First time posters: Do this! ]