A typical forward iterator is expected to implement following methods:
value_type& operator*();
value_type* operator->();
I’m writing a custom iterator for a custom container where user expects to see a value_type different from representation of the value inside a container. So when returning a value_type value to user I convert it from inner representation to user-expected value_type. It is easy to implement inside mentioned members.
Could you advice how to handle l-values? Where can I handle conversion of value_type to inner representation when user assigns values to iterator in syntax like *it = value_type(5)?
I thought of returning a functor but I dislike this idea becaues of non-usual syntax on caller’s side.
You might want to take a look into the implementation of the
std::vector<bool>specialization iterators, as they tackle the same problem. Note that with time, the specialization has been frown upon mainly because it does not comply with the container requirements (the iterators don’t provide references to the actual contained type, but a proxy value), and that will also be a problem with your implementation.The other possible approach is using a regular container but having the stored type accept implement the assignment/conversions to and from the user expected types. If that is not possible with your current stored type, you can write a wrapper for it.
A simplified wrapper (you will need to work on it to make it work):