Boost logo

Boost Users :

Subject: Re: [Boost-users] iterator_adaptor: making dereference of adapted tree node* return node value
From: Robert Jones (robertgbjones_at_[hidden])
Date: 2012-02-23 09:39:48

On Thu, Feb 23, 2012 at 9:57 AM, Rob Desbois <rob.desbois_at_[hidden]> wrote:

> I have the following iterator_adaptor type:
> template< class NODE >
> struct node_iterator: public boost::iterator_adaptor<
> node_iterator<NODE>, // Derived
> NODE*, // Base
> typename NODE::value_type // Value
> >
> {
> typename NODE::value_type dereference() const {
> return this->base_reference()->data();
> }
> };
> // and later:
> typedef detail::node_iterator<node_type> iterator;
> typedef detail::node_iterator<const node_type> const_iterator;
> This throws up the issue that in const_iterator, Value is not const,
> producing the following error:
> /usr/include/boost/iterator/iterator_facade.hpp:517:32: error:
> invalid initialization of non-const reference of type
> ‘container::detail::node_iterator<const container::detail::node<long
> unsigned int> >::reference {aka long unsigned int&}’ from an rvalue of
> type ‘container::detail::node<long unsigned int>::value_type {aka long
> unsigned int}’
> I could do some type traits magic with is_same, is_const and add_const
> to add the constness to Value if present on NODE, but is this the
> right way to do it?
> I've also thought about indirect_iterator - but adding operator*() to
> the node type seems not quite right. Perhaps transform_iterator...or
> is there some different method that might be more suitable?
I favoured the type traits magic approach, and publish the resulting value
type as the
value type of the iterator...

typedef typename if_<is_const<NODE>, const typename NODE::value_type,
typename NODE::value_type>::type value_type;

- Rob (J)

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at