Best practice for avoiding code duplication when implementing iterator and constant classes

What are the best methods to avoid code duplication when implementing pairs of classes such as iterator and const_iterator or the like?

  • Is an iterator usually implemented in terms of const_iterator using many const_casts?
  • Does any class of attributes use and ends up defining an iterator and const_iterator as different instances of a common template?

This seems like a fairly common problem for a canonical solution, but I have not found articles dedicated to this.

+6
source share
1 answer

I have no experience implementing iterators, although I think this is similar to other projects. Refactoring, common code, etc.

Looking at the GNU implementation of libstdc ++ std::vector::iterator

 #include <bits/stl_iterator_base_funcs.h> // ... template ... class vector : ... { typedef __gnu_cxx::__normal_iterator<pointer, vector> iterator; typedef __gnu_cxx::__normal_iterator<const_pointer, vector> const_iterator; }; 
+2
source

Source: https://habr.com/ru/post/1012145/


All Articles