Re: Something wrong with ustring::const_iterator



On Mon, 27 Apr 2009 10:30:14 +0200
Krzesimir Nowak <qdlacz gmail com> wrote:

> On Sat, 2009-04-25 at 09:51 +0200, François Legendre wrote:
> > #include <iostream>
> > #include <glibmm.h>
> > 
> > int main() {
> > 
> >         Glib::ustring str("Hello") ;
> >         for ( Glib::ustring::iterator it = str.begin() ; it !=
> > str.end() ; ++ it )
> >                 ;
> >         for ( Glib::ustring::const_iterator it = str.begin() ; it !=
> > str.end() ; ++ it )
> >                 ;
> >         return 0 ;
> > }
> 
> That doesn't work, because your str is not const, so it executes this
> method:
> iterator Glib::ustring::end();
> instead of:
> const_iterator Glib::ustring::end() const;
> 
> change in line 9:
> for ( Glib::ustring::const_iterator it = str.begin() ; it !=
> static_cast<Glib::ustring::const_iterator>(str.end()) ; ++ it )

This looks like a bug.  The STL provides a second version of
operator==() for its iterators which is templated on the types of both
arguments, to allow a const and non-const interator to be compared -
specifically to allow the form used by the OP, which is very common.

Chris


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]