Topic: Are the iterators to set<> const??
Author: gnb@bby.com.au (Gregory Bond)
Date: 1995/10/05 Raw View
I have discovered (to my chagrin) that all the iterators for the set<>
class template are const in the G++ 2.7.0 implementation. My copy of
the original Stepanov & Lee document (May 94) has this as a
specification, presumably because the set<> would need to tell if a
"key" member was changed via a non-const iterator.
My copy of the DWP (Apr 95), while (very!) short on detail for the
associative containers, does seem to indicate that non-const iterators
should be available. E.g. 23.3.3.1 has the following members for the
set<> template:
typedef typename Allocator::types<Key>::pointer iterator;
typedef typename Allocator::types<Key>::const_pointer const_iterator;
// .....
iterator begin();
const_iterator begin() const;
and the following phrase from S&L is not in the DWP:
"iterator is a constant bidirectional iterator referring to
const value_type".
I assume this is one of the changes to the STL proposal as part of the
standardisation process.
I can see four approaches to dealing with the problem of objects
contained in the set<> having "key" fields changed via non-const
iterators (and hence breaking the ordering of set<> members):
1 - Have the spec say, "Don't Do That." Fast & loose, the one true C way!
2 - Have the iterators detect assignment via funky helper-class glue
and "do the right thing" if key values change. This is fraught
with danger as the magic helper classes may not detect all writes
to the object (e.g. if a non-const reference or pointer escapes
somehow). It may also be quite slow. It may not even be
possible.
3 - Have the iterators detect assignment via funky helper-class glue
and disallow changes to key fields. Same problems as above.
4 - The DWP punts and reverts to the original S&L definition and
disallows non-const iterators.
Any clues on which way this will be resolved?
And (pending resolution or the adoption of #4) any way to update the
elements of a set<> without casting away const???? Or am I reduced to
using a set<> strictly as an index into a vector<>?
Greg, STL newbie.
--
Gregory Bond <gnb@bby.com.au> Burdett Buckeridge & Young Ltd Melbourne Australia
``Efforts to maintain the "purity" of a language only succeed in establishing an
elite class of people who know the shibboleths. Ordinary folks know better,
even if they don't know what "shibboleth" means.'' - Larry Wall
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]