The "," operator in for()s involving STL iterators

Martin v. Loewis
Fri Dec 31 20:54:00 GMT 1999

> I have a for loop,
> for ( int i = 0, vector<double>::iterator iter = a.start();
>        iter != a.end();
>        iter++, i++ )
> {
>     cout << (*iter) << endl;
> }
> Note the compound statement creating int i and the iter. A series of
> errors ensued as I moved the statements around ( attached )

Thanks for your bug report. This is a bug in your program, not in
gcc. What you want to do is not supported in C++.

According to the grammar:

      for ( for­init­statement condition-opt ; expression-opt ) statement


      decl­specifier­seq-opt init­declarator­list-opt ;

So you can have only one type in the for-init-statement, not multiple
types comma-separated. Also, when you write

   vector<double>::iterator iter = a.begin(), i = 0;

you declare two variables of type iterator: iter and i. This local i
hides any other variables named i that you might have. To have an
assignment to an existing i, you must use the expression-statement
variant of the for-init-statement.

Hope this helps,

More information about the Gcc-bugs mailing list