Boost logo

Boost :

From: Eric Friedman (ebf_at_[hidden])
Date: 2003-10-31 16:53:42


I have not followed the development of optional too closely, so I am
unsure of the current assignment semantics for optional<T>, let alone
optional<T&>. That said, comparison to variant might nonetheless be

Joel de Guzman wrote:
> What I would really like to see is this parallel:
> int a = 4;
> int& r = a;
> r = 3; // a is now 3
> <...>
> int a = 4;
> optional<int&> o(a);
> o = 3; // a is now 3
> Treating uninitialized lhs as initialization of the reference would
> rule out the assignment of constants and literals, right?

Well this sort of usage is explicitly disallowed with variant:

   int a = 4;
   variant<int&> v(a);
   v = 3; // compile error!

This is because I view variant as a "multi-type, single-value"
container. Thus, one must instead write something like the following:

   int a = 4;
   variant<boost::empty, int&> v(a);
   boost::get<int&>(v) = 3; // or use a visitor
   assert(a == 3);

My understanding is that this would map to

   int a = 4;
   optional<int&> o(a);
   *o = 3;

instead of your desired syntax.

In short, if optional<T&> were to mirror variant<empty, T&>, then
attempted use of optional<T&>::operator= should result in a compile error.


Boost list run by bdawes at, gregod at, cpdaniel at, john at