
What is a good way to create both an iterator and a const iterator while only having to specify all my custom behavior once?

On Sun, Mar 11, 2012 at 8:27 PM, John M. Dlugosz
What is a good way to create both an iterator and a const iterator while only having to specify all my custom behavior once?
Maybe template your iterator on the base iterator, and dispatching when necessary depending on whether that base iterator is the const iterator or the mutable iterator? E.g., template< class Base > struct my_iterator : boost::iterator_adaptor< Base > { }; typedef my_iterator< base_iterator > iterator; typedef my_iterator< base_const_iterator > const_iterator; HTH, - Jeff

What is a good way to create both an iterator and a const iterator while only having to specify all my custom behavior once?
Maybe template your iterator on the base iterator, and dispatching when necessary depending on whether that base iterator is the const iterator or the mutable iterator? E.g.,
template< class Base > struct my_iterator : boost::iterator_adaptor< Base > { };
typedef my_iterator< base_iterator > iterator;
typedef my_iterator< base_const_iterator > const_iterator;
More generally, template your iterator on whatever types will be different between the mutable and const versions (value type, reference type, etc.) The libstdc++ implementation of std::deque::iterator is an instructive example for doing this sort of thing. Regards, Nate

on Sun Mar 11 2012, "Jeffrey Lee Hellrung, Jr."
On Sun, Mar 11, 2012 at 8:27 PM, John M. Dlugosz
wrote: What is a good way to create both an iterator and a const iterator while only having to specify all my custom behavior once?
Maybe template your iterator on the base iterator, and dispatching when necessary depending on whether that base iterator is the const iterator or the mutable iterator? E.g.,
template< class Base > struct my_iterator : boost::iterator_adaptor< Base > { };
typedef my_iterator< base_iterator > iterator; typedef my_iterator< base_const_iterator > const_iterator;
http://www.boost.org/doc/libs/1_49_0/libs/iterator/doc/iterator_facade.html#... shows how to do it with iterator_facade, and http://www.boost.org/doc/libs/1_49_0/libs/iterator/doc/iterator_adaptor.html... extends that to iterator_adaptor HTH, -- Dave Abrahams BoostPro Computing http://www.boostpro.com
participants (4)
-
Dave Abrahams
-
Jeffrey Lee Hellrung, Jr.
-
John M. Dlugosz
-
Nathan Ridge