Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-11-15 11:08:03


From: "David Abrahams" <david.abrahams_at_[hidden]>
> From: "Peter Dimov" <pdimov_at_[hidden]>
> >
> > * ref(x), in a broader, lambda library context, is a function object
that
> > (regardless of the arguments passed to it) returns a reference to x,
like
> in
> >
> > ref(x) += _1;
>
> did you mean ref(x)() += _1?
> I don't see an argument being passed to ref(x)

Here's a more complete example:

int x = 0;

std::vector<int> v;

std::for_each(v.begin(), v.end(), ref(x) += _1);

ref(x) is a function object such that ref(x)(y) returns a reference to x.
_1(y) returns a reference to y. (E1 += E2)(y) returns E1(y) += E2(y). So

std::for_each(v.begin(), v.end(), ref(x) += _1);

means

for(iterator i = v.begin(); i != v.end(); ++i)
{
    (ref(x) += _1)(*i);
}

which is

for(iterator i = v.begin(); i != v.end(); ++i)
{
    ref(x)(*i) += _1(*i);
}

which translates to

for(iterator i = v.begin(); i != v.end(); ++i)
{
    x += *i;
}

Does this make more sense?

--
Peter Dimov
Multi Media Ltd.

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