[Bug libstdc++/19480] New: set::iterator is const

malessa at de dot ibm dot com gcc-bugzilla@gcc.gnu.org
Mon Jan 17 12:00:00 GMT 2005

The following piece of code gives me this compiler error:

itertest.C: In function `int main(int, char**)':
itertest.C:35: passing `const Foo' as `this' argument of `void
   Foo::setValue(unsigned int)' discards qualifiers
itertest.C:36: passing `const Foo' as `this' argument of `unsigned int
   Foo::getValue()' discards qualifiers

Here is the code:
#include <stdlib.h>
#include <iostream>
#include <set>

using namespace std;

class Foo
    Foo():mValue(0xFF) { }
    unsigned getValue() { return mValue; }
    void setValue(unsigned inValue) { mValue = inValue; }
    unsigned mValue;

int main(int argc, char **argv)
  set<Foo> fooSet;
  set<Foo>::iterator fooSetIter;

  fooSetIter = fooSet.begin();

  //This causes compile error with gcc:
  cout << fooSetIter->getValue() << endl;

I think this error comes up because set::iterator is defined as follows in 

  typedef typename _Rep_type::const_iterator iterator;
  typedef typename _Rep_type::const_iterator const_iterator;

So both iterator and const_iterator are defined as const_iterator !

Additional info:

bash-2.05b$ gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-
checking --host=i386-redhat-linux --with-system-zlib --enable-__cxa_atexit
Thread model: posix
gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7)

           Summary: set::iterator is const
           Product: gcc
           Version: 3.2
            Status: UNCONFIRMED
          Severity: critical
          Priority: P2
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: malessa at de dot ibm dot com
                CC: gcc-bugs at gcc dot gnu dot org


More information about the Gcc-bugs mailing list