What are the best practices to avoid code duplication when implementing class pairs such as iterator
and const_iterator
or similar?
- Does one usually implement iterator in terms of const_iterator using lots of const_casts?
- Does one employ some sort of traits class and end up defining both iterator and const_iterator as different instantiations of a common template?
This seems like a common enough problem to have a canonical solution but I have failed to find any articles dedicated to that.
value_type
,pointer_type
,reference_type
. For example:template <typename PType> class CustomIterator {};
typedef CustomIterator<MyClass*> iterator_type;
typedef CustomIterator<const MyClass*> const_iterator_type;
– Ben