|
Boost Users : |
Subject: Re: [Boost-users] iterator_adaptor: making dereference of adapted tree node* return node value
From: Rob Desbois (rob.desbois_at_[hidden])
Date: 2012-02-24 11:45:21
On 24 February 2012 15:25, Jeffrey Lee Hellrung, Jr.
<jeffrey.hellrung_at_[hidden]> wrote:
> On Fri, Feb 24, 2012 at 2:51 AM, Rob Desbois <rob.desbois_at_[hidden]> wrote:
>>
>> On 23 February 2012 18:58, Jeffrey Lee Hellrung, Jr.
>> <jeffrey.hellrung_at_[hidden]> wrote:
>> > 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:
>> >
>> >
>> > http://www.boost.org/doc/libs/1_48_0/libs/iterator/doc/iterator_facade.html#a-constant-node-iterator
>>
>> Actually iterator_facade/iterator_adaptor's value type in the
>> tutorials is the _node_ type, not the node's value_type.
>
>
> I never said anything to the contrary, AFAIK...
You didn't no, but in both iterator_facade and iterator_adaptor
tutorials, the dereference operation returns reference to node, not
reference to node data.
>> > 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.
>>
>> Thanks - I've updated to use
>> node_iterator::iterator_adaptor_::reference as the return type from
>> dereference(). The docs show that this defaults to Value&.
>> The error message still stands though, since const
>> node_type::value_type is still not const.
>
>
> Huh? I don't understand. "const node_type::value_type" *is* const-qualified.
Hehe sorry, that was clear as mud!
The code was roughly:
template<class T> struct node { typedef T value_type; /* ... */ };
template<class NODE> struct node_iterator: public node_adaptor<...,
/*Value = */ typename NODE::value_type> { /* ... */ };
So node_iterator<node<T>> has Value == node<T>::value_type == T
But in node_iterator<const node<T>> we have Value == (const
node<T>)::value_type == T - the parentheses show the const is bound to
the node, not the value type.
> IIRC, in your original post, your dereference member function returned an
> rvalue (based on the member function definition, not the actual return
> type). This suggests that the reference type you give to iterator_facade
> should be Value, rather than accepting the default of Value&.
Oh yes, didn't even spot that! IIRC the original code returned the
typedef 'reference', so was probably just a typo in the copying. Quite
a fundamental change though, so apologies for that >.<
--rob
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