Boost logo

Boost :

Subject: Re: [boost] [optional] Thoughts on disallowing assignment for wrapped references.
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-09-07 07:01:31


Fernando Cacciola wrote:
>
> >> optional<T>
> >> f()
> >> {
> >> optional<T> result;
> >> // do stuff
> >> return result;
> >> }
> >
> > However, in the latter case, there is no loss of generality
> > since the above would still be a no-no where T a reference
> > type...
> > Or did I miss something in your response?
>
> You totally missed his point.

Indeed.

> In Robert's example, it is easy to see--or should be easy to
> see--that the fact that the optional<T> is being returned is
> secondary, perhaps even irrelevant, to the point he is making
> His point is about the shortcoming of lacking assignment, not
> about object lifetimes (and BTW, is the same point Nevin and
> I made earlier).

I wouldn't go quite that far...

> Perhaps a less distracting example would be:
>
> optional<T> o ;
>
> switch ( cond )
> {
> case a : o = foo1(); break ;
> case b : o = foo2(); break ;
> case c : o = foo3(); break ;
> case e : o = foo4(); break ;
> case e : o = foo5(); break ;
>
> default : // o remains uninitialized
> }
>
> Notice that is quite possible to encounter the above example
> in real-life, as it is common that a variable remains
> uninitialized in the default case of a switch (or, for that
> matter, in any branch of a complex conditional).
>
> In fact, I even wonder if that example couldn't be considered
> as the canonical use case for optional<>'s assignment
> operation.

The one thing you overlook in your example is that there are reasonable alternatives to using optional within a local scope. However, when returning a value from a function, ownership must be transferred to the caller, which reduces the safe options.

Regardless, disallowing assignment for reference types which, in effect, "reseats a reference," is surprising in too many cases. A new, non-assignable optional type might gain traction, but removing assignment from optional<T&> is not acceptable.

_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer using std::disclaimer;
Dev Tools & Components
Susquehanna International Group, LLP http://www.sig.com

________________________________

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.


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