Boost logo

Boost Users :

Subject: [Boost-users] iterator_adaptor: making dereference of adapted tree node* return node value
From: Rob Desbois (rob.desbois_at_[hidden])
Date: 2012-02-23 04:57:45


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?

TIA,
--rob

--
Rob Desbois
Blog: http://theotherbranch.wordpress.com/
"I disapprove of what you say, but I’ll defend to the death your right
to say it", Voltaire

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