Boost logo

Boost Users :

Subject: Re: [Boost-users] iterator_adaptor usage question
From: Nathan Ridge (zeratul976_at_[hidden])
Date: 2012-03-12 00:31:10


>> 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
                                               


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net