The for-init-statement in a for statement
Hello everybody!
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. Does that mean that the
implementation of fill() above is not portable? Would it have to be
rewritten as follows to be standard compliant?
template <class _FwdIt, class _Ty> inline
void fill( _FwdIt _First, _FwdIt _Last, const _Ty& _Val )
{
while ( _First != _Last )
{
_First = _Val;
++_First;
}
}
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"?
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Toilet Tycoon
http://www.anvil-soft.de - Die Macher des Klomanagers
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]