Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2004-12-05 18:58:52


Tobias Schwinger wrote:
> David Abrahams wrote:
>
>> It's intentional. When your iterator's reference type is not
>> value_type& or value_type const&, your reference type is the same as
>> the value_type or you are using a proxy reference. Either way, if
>> your iterator is readable, the reference is implicitly convertible to
>> value_type. If your iterator is writable, it can be assigned from the
>> value_type. No persistent object (lvalue) of type value_type is
>> revealed by dereferencing the iterator; you can at best see a
>> temporary. If iterator_facde handed you a pointer to non-const
>> value_type, it would allow you to modify this temporary, which would
>> then disappear. We don't do that for the same reasons the language
>> won't bind a non-const reference to a temporary.
>
>
> I see - thanks for your detailed reply.
>
> When using proxy classes as references the assumption that modifications
> of a temporary object are lost at the end of this very object's lifetime
> does not apply

It most certainly does apply. There may be some cases in which it
doesn't, but the usual case is that it does.

> - modification of the proxy object actually means
> changing the stored data internally referenced by it.

Normally the reason for a proxy is that there isn't any "stored data
internally referenced by it." If there's a persistent lvalue, why not
just return a real reference instead of a proxy?

> In this case it would be OK if not preferable to have operator-> return
> a pointer to non-const...
>
> Is there any way to achieve this (besides hand-coding the whole iterator) ?

Just hand-code your own operator->.

> Or any hope that there will be a parameter to explicitly disable this
> extra-safety ?
>
> And why does operator* give me a mutable (temporary) object then, by the
> way ?

It is assumed that if you passed non-const R for the reference type, you
are trying to make a writable iterator, and R will have a (usually
non-const) assignment operator that takes a value_type argument. If we
returned a const object, it would be impossible to write

    *p = x;

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

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