Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2002-08-15 07:54:21


> I have a problem compiling the following code
> with g++ 2.95.3 and boost 1.28.0.
>
> ---%<---
> #include <boost/bind.hpp>
> #include <boost/function/function0.hpp>
>
> 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


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