Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-05-13 05:42:51


"Gennadiy Rozental" <gennadiy.rozental_at_[hidden]> writes:

> "Neal Becker" <ndbecker2_at_[hidden]> wrote in message
> news:c7uljb$etk$1_at_sea.gmane.org...
>> In iterator_facade.hpp:
>>
>> reference operator*() const
>>
>> Why the const?
>
> So you could dereference const iterators

I suppose we could provide a non-const overload, but that doesn't
sound like the right tool for this particular job.

>> I am writing function_input_iterator (again), using
>> iterator_facade. The functor used as a generator may not be const - it
>> might well have state. Therefore, dereference changes the state, and so
>> isn't const.
>
> Make'em mutable. Dereference is 'naturally' const operation. So special
> cases should be marked as special.

I think that's the right thing to do, but not quite the right
explanation. The reason to make the function object mutable is that
changes to the function object don't affect the "logical constness" of
the iterator. Any state in the function object only changes "once per
position" when it's embedded in the iterator, so that operator++ is
the effectively non-const operation. The following input iterator
requirements guarantee that operator* isn't allowed to advance the
iterator:

expression type semantics,pre/post-conditions
---------- ---- -----------------------------
*a convertible to T pre: a is dereferenceable.
                                If a==b and (a,b) is in the domain of ==
                                then *a is equivalent to *b.

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk