 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Matthias Hofmann Guest
|
Posted: Sat Jul 22, 2006 11:24 pm Post subject: 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! ] |
|
| Back to top |
|
 |
James Dennett Guest
|
Posted: Mon Jul 24, 2006 5:58 am Post subject: Re: The for-init-statement in a for statement |
|
|
Jiang wrote:
| Quote: | Please note expression
;
is a perfect valid expression.
|
It is not a valid expression; it is, however, a valid
expression-statement, as the expression in an
expression-statement is optional.
-- James
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|