Boost logo

Boost :

From: Joel (joel_at_[hidden])
Date: 2004-11-26 21:01:52


Zhuo Qiang wrote:
> I've just tried it and there's a question:
>
> int i = 1;
>
> let(_a = _1)
> [
> cout << --_a << ' '
> ](i);
> cout << i << endl;
>
> the output of above is : 0 0
> and I expected it to be "0 1"
> seems _a hold a "reference" to _1 which in turn is just a reference of i.Is
> it right?
> I thought _a = _1 had some value semantic.

That's right. That's the correct behavior. As noted in the docs:

The type of the local variable assumes the type of the
lambda-expression. Type deduction is reference preserving.
For example:

let(_a = arg1, _b = 456)

_a assumes the type of arg1: a reference to an argument,
while _b has type int.

This is necessary because otherwise, we won't have l-value
access to outer lambda-scopes.

> though the following code gives what I expect:
> int i = 1;
>
> let(_a = val(_1))
> [
> cout << --_a << ' '
> ](i);
> cout << i << endl;

Exactly! args are L-values. vals are R-values.

I'll emphasize that in the docs. Mind if I steal your example?

Regards,

-- 
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net

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