Topic: const_reverse_iterator & non-const containers


Author: Christian Millour <chris161@club-internet.fr>
Date: 1997/03/26
Raw View
an interesting problem was raised by Bob Sidebotham <rns@fore.com>
on gnu.g++.help. To summarize, g++ won't accept the following code
(as of libg++-2.7.2)

list<int> x;
for (list<int>::const_reverse_iterator i = x.rbegin();
     i != x.rend();
     ++x) {
}

it will however accept

list<int> x;
for (list<int>::const_iterator i = x.begin();
     i != x.end();
     ++x) {
}

the non-const versions of begin(), rbegin(), end() and rend() are
called, which is correct IMHO. What saves the day in the forward
case is that the libg++ version of stl provides custom definitions
for list<T>::iterator and list<T>::const_iterator, in particular
list<T>::const_iterator::const_iterator(const list<T>::iterator &),
which makes both initialization of i and comparison with x.end()
possible. This is OK with CD2 since both iterator and const_iterator
are implementation-defined.

However I don't think such a conversion-ctor can be introduced for
list reverse iterators, given their present definition in CD2. Maybe
a new template class std::const_reverse_iterator should be introduced
to complement
24.4.1.1  Template class reverse_iterator       [lib.reverse.iterator]
and be used to implement the specific containers const reverse
iterators (deque, vector, list, map..)

Now, I realize that the snippets above are not sufficient to mandate
alterations but I feel they might point to more serious deficiencies.
Or should both snippets above be outlawed ?

Comments ?
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]