This is the mail archive of the gcc-prs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: c++/10364: compile error using vector, comma operator and for loop


The following reply was made to PR c++/10364; it has been noted by GNATS.

From: "DeMarco, Paul" <pdemarco at ppg dot com>
To: 'Phil Edwards' <phil at jaj dot com>
Cc: gcc-gnats at gcc dot gnu dot org
Subject: RE: c++/10364: compile error using vector, comma operator and for
	 loop
Date: Wed, 9 Apr 2003 16:28:55 -0400 

 Shamed.
 I mis-copied the bites.begin()  as bites.begin
 
 :(
 
 -----Original Message-----
 From: Phil Edwards [mailto:phil at jaj dot com]
 Sent: Wednesday, April 09, 2003 4:11 PM
 To: DeMarco, Paul
 Cc: gcc-gnats at gcc dot gnu dot org
 Subject: Re: c++/10364: compile error using vector, comma operator and
 for loop
 
 
 On Wed, Apr 09, 2003 at 07:34:56PM -0000, pdemarco at ppg dot com wrote:
 > Here is a strange little bug where the addition of a comma operator kills the compile.  Below it works with 2 ints.
 > Regards.
 > --Paul
 > 
 
 You didn't include the error messages you received.  Here's my guess:
 
 
 > 	std::vector<string>::iterator oCMIter;
 > 	std::vector<string> bites;
 > 
 > 	for ( int iI = 3, oCMIter = bites.begin();
 > 				oCMIter != bites.end();
 > 				oCMIter++, iI++ )
 > 	{
 > 		cout << "hello" << endl;
 > 	}
 
 Buggy code.  The 'init' statement in a for loop can only be a simple
 declaration or an expression.
 
 "int iI = 3, oCMIter = bites.begin();" declares two integers, one
 called iI (initialized to 3) and another called oCMIter, initialized to
 bites.begin().  But there's no conversion from an iterator (the return
 value from bites.begin()) to an integer (the oCMIter being defined and
 initialized), so you get an error.
 
 The std::vector<string>::iterator oCMIter outside the for loop is shadowed
 by the int oCMIter inside the loop.  You would get a shadowing warning,
 /if/ it were legal code.
 
 Move the iI declaration out of the for-init-statement.  That way it becomes
 an expression instead of a declaration:
 
   	std::vector<string>::iterator oCMIter;
   	std::vector<string> bites;
     int iI;
   
   	for ( iI = 3, oCMIter = bites.begin(); ...
 
 
 Phil
 
 -- 
 To err is human; to forgive is simply not our policy.
     - MIT Assassination Club


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]