Boost logo

Boost :

From: Fernando Cacciola (fcacciola_at_[hidden])
Date: 2003-10-07 19:48:17


Hi Joel,

> Hi Fernando,
>
> Why wouldn't this compile?
>
> int x = 1;
> optional<int&> a;
> optional<int&> b(x);
> a = b; // << *** Here *** <<
>
> IMO, assignment should act like reference_wrapper:
>
> int x = 1;
> reference_wrapper<int&> a;
> reference_wrapper<int&> b(x);
> a = b; // OK
>
Good question! I can see by looking at the code that this is possible, though I'll have to play with it a little to understand
the semantics in this context.

With true references, these are a bit subtle since the effect is that the object to which the reference refers to is assigned
the rvalue (which if it happen to be of reference type it just behaves as an rvalue and not as an lvalue):

int a = 1 ;
int& ra = a ;
int b = 2 ;
int& rb = b;
ra = rb;
// a == 2 now, just as if I'd wrote: ra = 2; or ra = b ;

I'm not sure if the same will ocurr in the context of optional<T&> = optional<T&>

Fernando Cacciola


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