Boost logo

Boost Users :

From: Chris Uzdavinis (cuzdav_at_[hidden])
Date: 2007-02-03 09:55:24


On 2/3/07, abir basak <abirbasak_at_[hidden]> wrote:

> struct T2{
> int x;
> T2(int x) :x(x){}
};

> std::vector<boost::reference_wrapper<T2> > v;
> /// v[0].x = 5 ; this statement doesn't work!

> I am not sure why I have to use get for this purpose, while & operator
> is written for that purpose.

That is not why operator& is written, because the compiler has NO WAY
of knowing that your use of of the name "x" is referring to type T2,
and so it doesn't know to invoke that conversion operator first.
Instead, your code is trying to access member "x" of the
reference_wrapper itself.

The operator exists so you can initialize another reference from your
reference_wrapper:

T2 & t_ref = v[0];
t_ref = 5;

In this context, the compiler knows to utilize the conversion
operator, since you're assigning to a T2 reference.

Chris


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net