From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18308 invoked by alias); 9 Apr 2003 20:26:01 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 18293 invoked by uid 71); 9 Apr 2003 20:26:01 -0000 Date: Wed, 09 Apr 2003 20:26:00 -0000 Message-ID: <20030409202601.18292.qmail@sources.redhat.com> To: nobody@gcc.gnu.org Cc: gcc-prs@gcc.gnu.org, From: "DeMarco, Paul" Subject: RE: c++/10364: compile error using vector, comma operator and for loop Reply-To: "DeMarco, Paul" X-SW-Source: 2003-04/txt/msg00388.txt.bz2 List-Id: The following reply was made to PR c++/10364; it has been noted by GNATS. From: "DeMarco, Paul" To: 'Phil Edwards' Cc: gcc-gnats@gcc.gnu.org Subject: RE: c++/10364: compile error using vector, comma operator and for loop Date: Wed, 9 Apr 2003 16:25:05 -0400 Phil, My error message was something like that.. Here's another similar problem without the "for". With two examples in it. One with a string & one with vector assignment. I've even used parenthesis to ensure my intentions are explicit. This is prefectly valid C++ code. #include #include #include using namespace std; int main() { std::vector::iterator oCMIter; std::vector bites; int iI; string sYes; (iI = 3) , ( oCMIter = bites.begin ); // This WON'T COMPILE (iI = 3), ( sYes = "Yes" ); // this works fine. return 0; } My Error message was: foo.cpp: In function `int main()': foo.cpp:13: no match for `__gnu_cxx::__normal_iterator > >& = ' operator D:/MinGW/include/c++/3.2/bits/stl_iterator.h:571: candidates are: __gnu_cxx::__normal_iterator > >& __gnu_cxx::__normal_iterator > >::operator=(const __gnu_cxx::__normal_iterator > >&) Thanks, -- Paul -----Original Message----- From: Phil Edwards [mailto:phil@jaj.com] Sent: Wednesday, April 09, 2003 4:11 PM To: DeMarco, Paul Cc: gcc-gnats@gcc.gnu.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@ppg.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::iterator oCMIter; > std::vector 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::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::iterator oCMIter; std::vector bites; int iI; for ( iI = 3, oCMIter = bites.begin(); ... Phil -- To err is human; to forgive is simply not our policy. - MIT Assassination Club