Re: [boost] [optional] generates unnessesary code for trivial types

I see your point but, IMHO, optional<T&> is too different from normal references to attempt to draw this association. After all, optional<T&> is an object in the sense it has internal state and you can take its address while T& is not (one may call it an alias of an object). It is therefore logical that optional<T&>::operator= operates on the object (i.e. on the optional contents) and hypothetical T&::operator= operates on the referred object.
Note that if you store a normal reference to int as class member, you already have to write your assignment yourself. changing a normal reference to an optional reference should not come as something irregular.
Again, I tend to see optional<T&> as an object, with no apparent reason why it cannot be assigned to.
I understand (I think) your point of view. Let me clarify one thing. I am thinking of disabling the assignment not because I think it does not belong to references, but because there are two ways of implementing it, and implementing it either way would surprise a different group of programmers. And this would be a "run-time surprise". Instead my choice (not necessarily the best one) is to provide a "compile-time surprise". With your view of optional reference (if I got it right it is a pointer with a somewhat different syntax) your expectation of rebinding assignment comes as natural. With a different model of optional reference a non-rebinding assignment comes as more natural. I believe that optional<reference_wrapper<T>> would serve your purpose best. Or would it also introduce the lack of uniformity?

On Thursday, February 09, 2012 15:33:03 Andrzej Krzemienski wrote:
I believe that optional<reference_wrapper<T>> would serve your purpose best. Or would it also introduce the lack of uniformity?
Interesting. I'm not sure it's going to work because reference_wrapper won't have T's operators. The compiler may find the operators via ADL, but calling them will require an implicit cast from reference_wrapper to T&, which may mess up overload resolution. I think, optional<T&> is closer to my needs.

On Thursday, February 09, 2012 15:33:03 Andrzej Krzemienski wrote:
I believe that optional<reference_wrapper<T>> would serve your purpose best. Or would it also introduce the lack of uniformity?
One additional note on this. I would like optional<T&> to be perfectly implementable as a wrapper around T*. optional<reference_wrapper<T>> does not allow this, at least not with its generic interface, since optional<T>::get() returns T&, which effectively forces optional<reference_wrapper<T>> to store reference_wrapper internally (along with the value presence flag). Specializing optional on std::reference_wrapper does not solve the problem entirely because there is also boost::reference_wrapper. It would be odd if optional worked differently with different reference_wrappers.

on Thu Feb 09 2012, Andrzej Krzemienski <akrzemi1-AT-gmail.com> wrote:
I am thinking of disabling the assignment not because I think it does not belong to references, but because there are two ways of implementing it, and implementing it either way would surprise a different group of programmers. And this would be a "run-time surprise". Instead my choice (not necessarily the best one) is to provide a "compile-time surprise".
If value semantics are achievable for this kind of optional, I think it's better to supply that and let the people who don't expect value semantics be surprised at runtime. IMO-ly y'rs, -- Dave Abrahams BoostPro Computing http://www.boostpro.com
participants (3)
-
Andrey Semashev
-
Andrzej Krzemienski
-
Dave Abrahams