Boost logo

Boost :

From: Fernando Cacciola (fernando_cacciola_at_[hidden])
Date: 2005-10-17 16:33:29


Ian McCulloch wrote:
> [...]
>
> If optional<> is to be useful for references, then it should have the
> same semantics that a class acting as a proxy-reference has. ie, if
> I use optional<> with a class like
>
> template <typename T>
> struct Proxy
> {
> explicit Proxy(T& obj) : my_obj(&obj) {}
>
> Proxy& operator=(T const& obj) { *my_obj = obj; return *this; }
>
> // ...
>
> T* my_obj;
> };
>
> then optional<T&> should act as close to optional<proxy<T> > as
> possible.
>
If optional< proxy<T> > is used, for whatever proxy<T>, you can't assign a
value of type T to the optional.
You can only assign of value of proxy<T>, so it is the proxy assignment from
another proxy, or it's copy ctor, what counts. The proxy assignment from T
just won't participate.
So the proxy as you've shown here just won't compile in optional<> becasue
it has no copy ctor nor assignment from another proxy.
The assignment that you wrote there, albeit correct for the proxy<> itself,
just won't ever be used if you wrap it into optional<>.

> ie., if optional<T&> does something different just because
> the template argument is a reference

It doesn't.
optional<T&> is semantically equivalent to optional<reference_wrapper<T> >

> I wanted an optional<T&> - but I do find the rebinding behaviour
> surprising.
>
Because you got confused with one additional level of indirection.

Go through your example again and look what happens if a reference proxy is
used as an argument for optional<>.

Best

Fernando Cacciola
SciSoft
http://fcacciola.50webs.com/


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