C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The for-init-statement in a for statement

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Matthias Hofmann
Guest





PostPosted: Sat Jul 22, 2006 11:24 pm    Post subject: The for-init-statement in a for statement Reply with quote



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





PostPosted: Mon Jul 24, 2006 5:58 am    Post subject: Re: The for-init-statement in a for statement Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.