Boost logo

Boost Users :

Subject: Re: [Boost-users] How can I save the values in lambda expression? Can any one with kindness help me?
From: Zachary Turner (divisortheory_at_[hidden])
Date: 2009-07-09 00:12:26


2009/7/8 fmingu <fmingu_at_[hidden]>:
> As you know, I am a Chinese and according to my knowledge few book in
> Chinese introduce boost library. I only have the book " Beyond the C++
> Standard Library: An introduction to boost" (written by Bjoern Karlsson,
> translated in Chinese).Only several pages said about lambda. I tried to ask
> www.csdn.net( a Chinese developers' net) and few people answered my
> questions. I have not studied the source code of lambda yet. So all I have
> to do is to ask www.boost.org to get answers and go forth.
> I think the var(switchvalue) is a pointer to a function from the example
> code in the book:
> ..........
> std::for_each(vec.begin(),vec.end(),var(m)=_1);
> ..............
> So the operations in m can be performed. Am I right?
> But I do not know how to save the values in lambda expression.
> Thanks a lot.

Try to do only

std::cout << switchvalue << std::endl;

The value has already been saved, there is no need to use var outside
of the lambda expression. for example, in the for_each code you
posted, the reason to use var is because

std::for_each(vec.begin(),vec.end(), m=_1);

is not legal. However, using var it will basically have the same
effect. Once the lambda function has executed, m directly contains
the updated value and you can access it like any variable.


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