Boost logo

Boost :

From: Anthony Williams (anthony_w.geo_at_[hidden])
Date: 2005-10-18 10:56:54

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. Instead, to
change the binding, use reset(), and to assign use *.

optional<int> x;
x.reset(3); // x now holds the value 3
x.reset(4); // x now holds a new int, value 4
*x=5; // change the value of the held int, to 5
x.reset(); // x now empty
x=6; // error, no direct assignment
x=optional<int>(6); // now holds a new int, value 6

struct NonAssignable
    const int value;
    NonAssignable(int value_):

optional<NonAssignable> y;
y.reset(NonAssignable(3)); // ok
y.reset(NonAssignable(4)); // y now holds a new NonAssignable, value 4
*y=NonAssignable(5); // error, NonAssignable doesn't support assignment
y.reset(); // y now empty
y=NonAssignable(6); // error, no direct assignment
y=optional<NonAssignable>(NonAssignable(6)); // now holds a new NonAssignable

int a=1;
int b=2;
int c=3;
optional<int&> z;
z.reset(a); // ok --- reference to a
z.reset(b); // ok --- reference to b
*z=c; // assign 3 to b
y=c; // error, no direct assignment
y=optional<int&>(c); // now holds a ref to c


Anthony Williams
Software Developer
Just Software Solutions Ltd

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