Martin Wartens wrote:
Hi,
I came up with a solution that seems to work. It is as simple as adding "const" to the Value template parameter of the iterator_adapter.
Yes, I could (and should) have told you that would work. But it's not a good answer. The library really ought to do the right thing for this sort of iterator.
Then I can have a non- const iterator that always returns const when dereferenced. Unfortunately, the iterator_adapter class has a public member function "base" that returns the underlying iterator. Since I want to prevent public access to the underlying operator, I had to tweak iterator_adapter and make this function protected.
The right answer for that case is not to advertise the function to your clients. If they don't know it's there, they can't call it. We could consider making it a free function in the library, but I don't see how that could be any more "hidden." Your FriendHelper is just as publicly accessible as the base() member function is today. You can also make it private in your derived class, or hide it with another member named "base." That would make it inaccessible unless your clients actually cast your iterator to its base class.
Then I want to give access to the base function only to the container adaptor (a map adaptor in my example). Providing the friend access in a generic way is a little bit awkward, as it has to be done through a helper class. Some sample code for this is given below. I have not yet fully explored this solution, I hope I won't face any nasty surprises.
-- Dave Abrahams Boost Consulting http://www.boost-consulting.com