
David Abrahams wrote:
Anthony Williams <anthony_w.geo@yahoo.com> writes:
How's this for a radical thought --- optional<T> should not be assignable from T. That way, it doesn't matter whether T is a reference, or not.
There's at least some precedent for types that are "immutable except for copy assignment."
The assignment from T is really a kind of mutating operation.
Indeed. I'm leaning fast into dropping assignment form T. That definitely solves the reference binding issues.
Instead, to change the binding, use reset(), and to assign use *.
To make that valid for a null optional, I think operator* would have to return a proxy reference... which is not an altogether bad idea, IMO.
That would actually bring back the problem as assignment to a null reference would bind and then there's the question about what to do with assignment to a non-null reference (rebind as I proposed or not-rebind as Joel proposed) But if we do as both Sam Partignton and Antony Williams suggest, drop assignment from T altoghether, the situation looks a lot more clear, at least to me. Antony's proposal goes even beyond Sam's idea: rather than dropping assignment from T, we spell it: reset(). That is definetly better then direct assignment from T, but I'm not quite sure that is as good a solution as keeping just copy assignment. Consider these 3 versions: int a = 1; int b = 2; int& ra = a ; int& rb = b ; optional<int&> o(ra); *o = rb ; // Clearly doesn't rebind but is UB if 'o' were null. // (1) Current case: o = rb ; // rebind or not?? // (2) Antonty's proposal: o.reset(rb); // still some room for doubts? // (3) Sam's proposal: o = make_optional(rb); // Clearly rebinds, doesn't it? // I just introduced make_optional() here to get rid of the template argument I think (3) spells the actual semantics (rebinding) even more clearly than (2). OTOH, both (2) and (3) are equivalent, so the difference is merely sintatical. Having said that, I prefer (3) which just looks better to my eyes, but (2) could work too. Best -- Fernando Cacciola SciSoft http://fcacciola.50webs.com/