|
Boost : |
From: Rob Stewart (stewart_at_[hidden])
Date: 2005-10-18 10:34:41
From: "Fernando Cacciola" <fernando_cacciola_at_[hidden]>
>
> I wonder what would be the result of the following thought experiment:
> Please, answer the questions _without_ looking at the answers, and if
> possible post your initial results; then compare with the answers.
IANAOU either (yet!), but I'll give my answers.
First, I'll describe what I think the right behavior is; this may
be exactly how optional works now or it may not be. I haven't
compared this description to optional's documentation.
An optional<T&> should optionally contain a reference to T.
Either optional<T&> has a T& or it doesn't. Since T& cannot be
rebound, neither should optional<T&>'s T& be rebound by
assignment from another T&. That said, assigning from none
restores the optional<T&> to (the equivalent of) its default
constructed state, so it can bind to a new T&.
Thus, I don't consider the described behavior for optional<T&> as
supporting rebinding because it is reset by assignment from none;
it is not rebound by assignment from a T&.
> int a = 1;
> int b = 2 ;
> int c = 3 ;
> int &ra = a;
> int &rb = b;
> int &rc = c;
>
> optional<int&> o = ra;
You can't have a reference to a reference, so o contains a
reference to a.
> o = rb ; // [1]
Assigns b's value through o's reference to a. o still holds a
reference to a.
> o = none ;
No reference in o.
> o = rc ; [2]
Now o contains a reference to c.
> (a) What is the value of 'a' after [1]?
2
> (b) What is the value of 'a' after [2]?
2
-- Rob Stewart stewart_at_[hidden] Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk