Boost logo

Boost Users :

From: Stuart Dootson (stuart.dootson_at_[hidden])
Date: 2006-01-24 11:58:05


On 1/24/06, David A. Greene <greened_at_[hidden]> wrote:
> I'm having some trouble with BLL and Boost.Function in this simple
> testcase:
>
> #include <boost/lambda/bind.hpp>
> #include <boost/function.hpp>
>
> #include <iostream>
>
> class Test {
> public:
> void foo(int a) { std::cout << "a is " << a << std::endl; };
> };
>
> template<typename Function>
> class Caller {
> private:
> Function func;
>
> public:
> Caller(Function f) : func(f) {};
>
> void bar(void) { func(); };
> };
>
> int main(void)
> {
> Test tester;
> int value = 10;
>
> Caller<boost::function<void (void)> >
> printer(boost::lambda::bind(boost::lambda::bind(&Test::foo, tester,
> boost::lambda::_1), value));
>
> printer.bar();
>
> return(0);
> }
>
> The g++ 4.0.3 compiler complains (full trace below):
>
> /usr/include/boost/lambda/detail/lambda_functor_base.hpp:408: error:
> invalid use of void expression
>
> Is it not legal to bind a lambda-bound function?
>
> -Dave

It's probably more that it makes the compilers job too
difficult...anyway - I think you'll find that one of these two does
what you want (I've assumed a 'using namespace boost::lambda;' because
it declutters the code):

   Caller<boost::function<void ()> > printer1(bind(&Test::foo, &tester, value));
   Caller<boost::function<void ()> > printer1(bind(&Test::foo,
&tester, boost::ref(value)));

With printer1, the value of 'value' is baked into the bound object.
With printer2, a reference to 'value' is baked into the bound object.
This means that if you change the value of 'value', printer1 won't
reflect the change, but printer2 will - the following code

   printer1.bar();
   printer2.bar();
   value = 1234;
   printer1.bar();
   printer2.bar();

will give the output

   10
   10
   10
   1234

HTH!
Stuart Dootson


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