Re: Something wrong with ustring::const_iterator



On Mon, 27 Apr 2009 11:38:44 -0400
Hubert Figuiere <hub figuiere net> wrote:
> 
> On 27/04/09 04:30 AM, Krzesimir Nowak wrote:
> > 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;
> 
> No. The C++ lookup for the method would determine the method based on 
> the return type first, and then check for const-ness, which is in
> that case fine as non-const -> const is allowed.

Krzesimir is right.  It is the comparison operator which is the problem.
Glib::ustring::iterator is of type Glib::ustring_Iterator<std::string::
iterator>, and Glib::ustring::const_iterator is of type Glib::
ustring_Iterator<std::string::const_iterator>.

There is a mismatch of types when the templated function operator!=() is
instantiated because of the different cv qualifiers of the iterators to
be compared.  The qualifier applying to the temporary created by the call
to Glib::ustring::end() is based on the constness of the ustring object,
and it is non-const.

Snip the test case below out, and compile it with and without the two
argument templated version of operator !=().  If you comment it out it
won't compile.  (Compare with the single templated version in ustring.h)

Chris

-------------------><O ---------------

#include <glibmm/ustring.h>
#include <iostream>
#include <ostream>

namespace Glib {
template <class T1, class T2> inline
bool operator!=(const ustring_Iterator<T1>& lhs, const ustring_Iterator<T2>& rhs) {
  return (lhs.base() != rhs.base());
}
} // namespace Glib
 
int main() {
  Glib::ustring str("Hello");
  for (Glib::ustring::const_iterator it = str.begin(); it != str.end(); ++it )
    std::cout << (char)*it << std::endl;
  return 0 ;
}


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