Boost logo

Boost Users :

Subject: Re: [Boost-users] iterator_adaptor: making dereference of adapted tree node* return node value
From: Jeffrey Lee Hellrung, Jr. (jeffrey.hellrung_at_[hidden])
Date: 2012-02-23 13:58:05

On Thu, Feb 23, 2012 at 1: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?

To reiterate Robert Jones' reply, yes, AFAIK, a const iterator should pass
a const-qualified value_type to iterator_facade/iterator_adaptor, as
implied by the constant node_iterator example in the docs:

On a related note, you should specify your reference type you pass to
iterator_adaptor explicitly as the return type of your dereference member
function. Otherwise I think you'll end up returning a reference to a
temporary within operator*. I believe that's *actually* what the quoted
error is trying to tell you.


- Jeff

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