[prev in list] [next in list] [prev in thread] [next in thread] 

List:       hpux-cxx-dev
Subject:    Re: CXX-DEV: odd const_iterator compile-time error
From:       Alain Miniussi <alainm () cup ! hp ! com>
Date:       2000-05-31 18:52:45
[Download RAW message or body]

"Christopher A. Larrieu" wrote:
> 
> Hi,
> 
>   Listed below are the compile error and the code which
> precipitated it.  Note that both methods are the same,
> with the exception that f1 is declared const.  Adding
> the const somehow avoids the error, but why?  Similarly,
> the error can be avoided by using an iterator rather
> than a const_iterator, but why?

The root of the problem is that you can't compare const and
non const iterator, or, more correctly, that it is unspecified:
Comparison betwen the same flavor of iterators must be 
provided, and initialisation of const iterator by a non const
must be provided, then, depending on the fact that the operators
are implemented in terms of members or not (which is the unspecified
part) you can or cannot compare const and non const.

So, it's safe to remember that comparison of const and non const 
iterators is non portabe (some people consider that this is
wrong and that the standard should be fixed in that 

By qualifiy f1 const, you say that "this" points to a const object,
and that the members are to be considered const in that method.

For obvous reason, the const version of begin returns a const
iterator and the non const begin return a non const iterator,
and same thing for end().

so:

void      f1    (void) const
>   {
>     list<string>::const_iterator it = items.begin();
>     for (; it != items.end(); ++it)
                   ^^^^^^^^^
f1 is const -> don't modify items -> items.end() is a const iterator


>   void      f2    (void)
>   {
>     list<string>::const_iterator it = items.begin();
>     for (; it != items.end(); ++it)
 f2 non const -> can modify items -> items.end() is a non const
iterator,
can't be compared with a const iterator in a portable way.

Alain
 _________________________________________________________________
 To leave this mailing list, send mail to majordomo@cxx.cup.hp.com
    with the message UNSUBSCRIBE cxx-dev
 _________________________________________________________________

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic