Boost logo

Boost :

From: Joel de Guzman (joel_at_[hidden])
Date: 2005-10-17 07:14:54


Peter Dimov wrote:
> Joel de Guzman wrote:
>
>
>>Oh, I forgot:
>>http://boost.org/libs/optional/doc/optional.html#refassign
>>
>>To this day, I'm still befuddled at the explanation.
>
>
> What's wrong with it?
>
> "Rebinding semantics for the assignment of initialized optional references
> has been chosen to provide consistency among initialization states..."

There's nothing wrong with that. That's clear. You've snipped out
the other parts though. What's not clear is why code like that
posted by AlisdairM should assume the behavior:
http://lists.boost.org/Archives/boost/2005/10/95487.php

A gotcha AFAICT.

> seems pretty clear to me. Since assignment to an uninitialized optional<T&>
> rebinds, so does assignment to initialized optional<>.
>
> Consider the analogy with variant<T&, U&>. What does assignment do? Under
> the "always rebind" model it rebinds. Under the "behave as a reference"
> model it depends on the argument type and the current state of the variant.
> It may assign to the referenced object, or it may rebind.

No. Under the "behave as a reference" model, references should
bind only on construction. Everything else is assignment to the
referenced object. No rebind.

> "Always rebind"
> certainly appears more consistent to the untrained eye.

Really? To me this is definiteley wrong no matter how you put it:

     int a = 13;
     int &b = a;
     int c = 42;

     optional< int & > d = b;
     d = c; // rebind!

Ok, this is definitely my last post on the issue.

Cheers!

-- 
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net

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