Topic: iterator and const_iterator relationship


Author: Stephen Cleary <scleary@jerviswebb.com>
Date: 2000/07/12
Raw View
On Sat, 8 Jul 2000 03:10:55 CST, Kevin Kostrzewa <tkkost@newsguy.com>
wrote:

>2) Is there any mention in the standard at all about the relationship
>   between iterators and const_iterators (that an iterator can be
>   "demoted" to a const_iterator, etc.) or should I make sure that I'm
>   always *using* iterators with iterators and const_iterators with
>   const_iterators?  Would an implementation be (strictly speaking)
>   legal if they defined an iterator and a const_iterator on a
>   container to be two wholly separate classes that had no
>   relationship with each other (not comparable, can't assign an
>   iterator to a const_iterator, etc.)

Nothing right this instant.  However, this problem has been noticed;
it's Library Active Issue #179 at:
  http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/lwg-active.html

 -Steve


Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Kevin Kostrzewa <tkkost@newsguy.com>
Date: 2000/07/08
Raw View
Is there any place in the standard that formalizes the relationship
between a container's iterator and const_iterator?

The reason why I ask is that I'm used to iterating through a container
using the weaker form (const_iterator) if I don't need a mutable
iterator, just so I don't do anything stupid and modify a value when I
didn't mean to.

For example:
std::map<int,int> container;
std::map<int,int>::const_iterator ii = container.begin();
if (container.end() != ii)
        ...
        // note - container.end() returns an iterator,
        // not a const_iterator

That kind of code was working hunky-dory on std::map containers using
VC++6 (SP3) with some extra patches to the implementation header file
xtree provided by dinkumware (the standard library provider for
VC++) [see note below on why this worked].  I just downloaded the
latest patch to VC++6 (SP4) and that code no longer compiles.  The
error comes from the "if (container.end() != ii)" line.

std::map::iterator is derived from std::map::const_iterator (as I
would expect), but there is no global operator==/!= for
std::map::const_iterator, only class operator==/!= for both
std::map::iterator and std::map::const_iterator.  The class
operator==/!= operator for std::map::iterator hides the class
operator==/!= operator for std::map::const_iterator (the baseclass),
which prevents comparison of an iterator on the left hand side with a
const_iterator on the right hand side of an expression.

Here's how my question relates to the standard:

1) I contemplated adding my own global operator== and operator!= to
   get around this problem and allow my code to compile.  Is this more
   in line with the standard, or is it more in line with the standard
   to change my coding practice and make sure that I'm always
   *comparing* iterators with iterators and const_iterators with
   const_iterators?

2) Is there any mention in the standard at all about the relationship
   between iterators and const_iterators (that an iterator can be
   "demoted" to a const_iterator, etc.) or should I make sure that I'm
   always *using* iterators with iterators and const_iterators with
   const_iterators?  Would an implementation be (strictly speaking)
   legal if they defined an iterator and a const_iterator on a
   container to be two wholly separate classes that had no
   relationship with each other (not comparable, can't assign an
   iterator to a const_iterator, etc.)

[note]
The code that compared iterators on the left hand side to
const_iterators on the right hand side worked because dinkumware's
patch to xtree that was posted on their web site (not included in
VC++6 SP4) had a bug where std::map::const_iterator was derived from
std::map::iterator.

--
kevin kostrzewa
work: kkostrzewa@csisolutions.com
home: tkkost@newsguy.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]