Boost logo

Boost Users :

From: Simmons, Aaron (a-simmons1_at_[hidden])
Date: 2005-08-10 11:44:14


I'm trying to use boost::bind and boost::function with std::logical_not
and I've run into some strange template magic that I don't understand.
In the example below, I use boost::function to help compose several
boost::bind calls so that I don't have to do it all on one line (I find
that this improves readability). The problem is that when I do this,
the functions that are bound with boost::bind never get called. They
will get called only if I construct everything on one line. (???)

 

Is this expected behavior? If so, why does it work like this?

 

 

Thanks,

aaron

 

 

 

#include <algorithm>

#include <functional>

#include "boost/bind.hpp"

#include "boost/function.hpp"

 

using namespace std;

using namespace boost;

 

class test {

public:

    void bindtest() {

        // test1: composed gradually for clarity

        //

        function<bool ()> funcFoo = bind(&test::foo, this);

        function<bool ()> funcBar = bind(&test::bar, this);

        function<bool ()> funcTest1= bind(logical_and<bool>(), funcFoo,
funcBar);

 

        // doesn't call test::foo or test::bar! (returns true)

        bool test1= funcTest1();

        cout << "test1= " << test1 << endl;

 

        // test2: composed directly on one line

        function<bool ()> funcTest2= bind(logical_and<bool>(),

                                          bind(&test::foo, this),

                                          bind(&test::bar, this) );

 

        // calls test::foo and test::bar (returns false)

        bool test2= funcTest2();

        cout << "test2= " << test2 << endl;

    }

 

    bool foo()

    {

        cout << "foo" << endl;

        return false;

    }

 

    bool bar()

    {

        cout << "bar" << endl;

        return false;

    }

};

 



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