|
Boost Users : |
From: Martin Wartens (martin.wartens_at_[hidden])
Date: 2004-11-30 17:43:47
Hi all,
I am trying to disguise a std container non-const iterator as a
const_iterator. My guess is that iterator_adaptor could be useful for this,
but I can't figure it out myself.
I am trying to solve an old problem with the standard library, which is
that you can't convert a const_iterator into an iterator. [There are two
"solutions" to that problem, one uses distance and is expensive, the other
one does a brutal conversion on bitlevel which breaks for several std
implementations.]
My use case: I have written a wrapper class mymap for std::map. I only want
to give map::const_iterators to the users of this class. If the user wants
to erase an element from the map, he must pass a const_iterator back to the
wrapper class and call "mymap::erase(const_iterator)". The problem is that
mymap has no way to convert the const_iterator into an iterator to call
"map::erase(iterator)" in turn [there is no "map::erase(const_iterator)"].
So my idea was to write a iterator_adaptor for iterator that lets it behave
like a const_iterator. Unfortunately I didn't get very far. I have to say
that the documentation was not of much help to me. There are lots of
complicated implementation details but only small examples. So any help is
appreciated. How can I solve my problem?
Side note: In the following code sample, iterator_adaptor accidentally
turns a const_iterator into a non-const iterator. (Seemingly, some
bit-level casting is done somewhere). Is this a bug?
#include <boost/iterator/iterator_adaptor.hpp>
#include <map>
template <class Value>
class node_iter
:public boost::iterator_adaptor<node_iter<Value>, Value>
{
public:
node_iter()
: node_iter::iterator_adaptor_(0) {}
node_iter(Value p)
: node_iter::iterator_adaptor_(p) {}
};
int main()
{
typedef std::map<int, int> mymap_type;
mymap_type testmap;
node_iter<mymap_type::const_iterator> ni(testmap.begin());
ni->second = 42; //this works, not a const_iterator anymore
}
___________________________________________________________________________
Die einen haben es, die anderen werden es!
http://my-mail.ch/?redirect=9905
So macht schenken richtig Freude! Schenken und Helfen mit World Vision!
http://www.my-mail.ch/?redirect=1958
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net