I have a problem compiling the following code with g++ 2.95.3 and boost 1.28.0.
---%<--- #include
#include struct A { A(boost::function0<void> const &) { } };
void foo(boost::function0<void> const &) {} void bar(unsigned int) {}
int main(int argc, char *argv[]) { boost::bind(foo, boost::bind(bar, 0)); // ok A a1(boost::bind(bar, 0)); // ok A a2(boost::bind(foo, boost::bind(bar, 0))); // error return 0; } --->%---
The line marked with error gives me a whole flood of error messages. Is this a problem with my version of gcc? Or am I doing a stupid mistake?
A mistake, but not a stupid one. The boost::bind(bar, 0) subexpression is interpreted as a nested bind; that is, bind assumes that you want foo(bar(0)), but you actually want foo(bind(bar, 0)). You can "protect" the nested bind from being evaluated by using the helper function protect() defined in boost/bind/protect.hpp; see the last two paragraphs of http://www.boost.org/libs/bind/bind.html#nested_binds