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