Re: The for-init-statement in a for statement
Matthias Hofmann wrote:
When I first learned learned C++, I often encountered statements like
for ( ;; ) {}
instead of
while ( true ) {}
Also, I found code in the VC++ implementation of STL
algorithms that omitted the for-init-statement in for
statements, for example in:
template <class _FwdIt, class _Ty> inline
void fill( _FwdIt _First, _FwdIt _Last, const _Ty& _Val )
{
for( ; _First != _Last; ++_First )
_First = _Val;
}
This seems to make perfect sense, but 6.5.3/1 of the C++
Standard says that the for-init-statement cannot be omitted.
It most certainly can't. And isn't, in either of the above
cases.
The for-init-statement can be an expression statement, however,
and the expression in an expression statement is optional.
[...]
By the way, I noticed that my version of the C++ Standard
defines the for statement as follows:
for ( for-init-statement condition opt ; expression opt ) statement
I guess there is a semicolon missing right before "condition"?
Not at all. A for-init-statement can be either a declaration or
an expression statement, and both of these end with a semicolon.
The original C did not allow a declaration here, and the grammar
was:
for ( expression[opt] ; expression[opt] ; expression[opt] ) statement
When the language was changed to allow a declaration (which is a
statement) as the first term, the grammar was changed to what
you now see (except that conditional didn't replace the second
expression until much later). Since the statement contains a
semi-colon, it cannot be part of the grammar of the for
statement (or you would need two semi-colons).
--
James Kanze kanze.james@neuf.fr
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]