Boost logo

Boost Users :

From: Alan M. Carroll (amc_at_[hidden])
Date: 2005-08-10 12:26:50

I don't know if it's expected, but my tests indicate that the problem is that the bind() of funcTest1 is storing two constant bools, not the function<> objects. You can demonstrate this by

1) not initializing funcFoo. Note that the result from funcTest1 is now false but there is no exception from using an empty function<> object.
2) setting funcFoo and/or funcBar to 0 after initializing funcTest1. Note that this has no effect on calling funcTest1.

I suspect that the proximate cause is that you can use function<> objects as booleans, to check if they are not empty. bind() sees this and therefore treats them as things to evaluate to be bools to store in the bind, not as function objects to be invoked later. I don't have a good workaround at this time.

At 11:44 AM 8/10/2005, Simmons, Aaron wrote:
Content-class: urn:content-classes:message
Content-Type: multipart/alternative;

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?
#include <algorithm>
#include <functional>
#include "boost/bind.hpp"
#include "boost/function.hpp"
using namespace std;
using namespace boost;
class test {
void bindtest() {
// test1: composed gradually for clarity
        function< bool ()> funcFoo  = bind(&test::foo, this);
bool ()> funcBar  = bind(&test::bar, this);
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 >(),
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 mailing list

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at