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;
         boundary="----_=_NextPart_001_01C59DCA.BE537430"

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 mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users