Topic: ANSI draft, set<> and iterators


Author: klamer@ph.tn.tudelft.nl (Klamer Schutte)
Date: 1995/07/03
Raw View
Given the following file:

>>>>>>>>>> start of r3.cc
#include        <set.h>

struct A { int a, b; };
bool    operator<(const A &x, const A &y) { return x.a < y.a; }
set<A, less<A> >        sa;

void
test() {
        set<A, less<A> >::iterator i = sa.begin();
        (*i).b++;
}

int
main() {
  test();
  return 0;
}
<<<<<<<<< end of r3.cc

The ANSI draft paper does not disallow the code above. However, in the
original STL paper it was stated that an iterator of a set<> is the same
as a const_iterator, and thus that (*i) should be const.

To solve this problem, I have two proposals:

1) Add to the ANSI paper the fact that the iterator of a set is in fact
   a const_iterator.

2) Take the iterator out of a set<>. If both an iterator and a const_iterator
   is supplied, the naive user is given the idea that a const_iterator cannot
   change the object pointed to, and an iterator can. However, it is clear
   that it cannot be allowed in a set that the object pointed to is changed,
   as this possible would change the order of the elements in the set.
   A logical decision from this is to offer only a const_iterator for a set<>
   and not an iterator.

Klamer
--
Klamer Schutte -- +31-15-786054 -- klamer@ph.tn.tudelft.nl
http://www.ph.tn.tudelft.nl/People/klamer/Klamer.html