[Boost-users] [lambda] delayed assignment to a member

22 views
Skip to first unread message

Igor R

unread,
Oct 29, 2009, 1:16:11 PM10/29/09
to boost...@lists.boost.org
Hello,

How can I create a functor, which would assign to a member variable,
when invoked? I mean, something like this:

int member_;
//...

lambda::bind(my_obj::member_, this, 5);

The above bind itself compiles, but if I try to invoke it or to assign
to a function<void()>, it doesn't compile.

Thanks.
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Steven Watanabe

unread,
Oct 29, 2009, 1:23:35 PM10/29/09
to boost...@lists.boost.org
AMDG

Igor R wrote:
> How can I create a functor, which would assign to a member variable,
> when invoked? I mean, something like this:
>
> int member_;
> //...
>
> lambda::bind(my_obj::member_, this, 5);
>
> The above bind itself compiles, but if I try to invoke it or to assign
> to a function<void()>, it doesn't compile.
>

(lambda::var(member_) = 5)

In Christ,
Steven Watanabe

Igor R

unread,
Oct 29, 2009, 3:03:02 PM10/29/09
to boost...@lists.boost.org
> (lambda::var(member_) = 5)

Did you mean &my_obj::member_? But anyway, it doesn't compile.
My real code should look like this:

io_service.post(lambda::var(member_)=5); // posts to another thread
and performs the assignment there

Steven Watanabe

unread,
Oct 29, 2009, 3:15:35 PM10/29/09
to boost...@lists.boost.org
AMDG

Igor R wrote:
>> (lambda::var(member_) = 5)
>>
>
> Did you mean &my_obj::member_?

No. I meant exactly what I said.

> But anyway, it doesn't compile.
>

The following works fine.

#include <boost/lambda/lambda.hpp>
#include <boost/function.hpp>
#include <cassert>

struct S {
int member_;
void f() {
boost::function<void()> f_(boost::lambda::var(member_) = 5);
f_();
}
};

int main() {
S s;
s.f();
assert(s.member_ == 5);
}

> My real code should look like this:
>
> io_service.post(lambda::var(member_)=5); // posts to another thread
> and performs the assignment there
>

In Christ,
Steven Watanabe

Reply all
Reply to author
Forward
0 new messages