Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2004-06-02 13:24:32

Agoston Bejo wrote:
> Hi,
> Please take a look at the code below.
> The point is that for_each_treenode calls for_each_treenode_child. It
> seems to me that
> bind(Print, ref(cout), _1)(&t);
> is not the same as
> Print(cout, &t);
> But if this is the case, how come that
> for_each_treenode_child(&t, bind(Print, ref(cout), _1));
> compiles and works without problem?


> Print(cout, &t); // WORKS
> bind(Print, ref(cout), _)(&t); // GENERATES ERROR (1)

The error is caused by the fact that &t is an rvalue.

Tree * pt = &t;
bind(Print, ref(cout), _1)(pt); // should work

> for_each_treenode_child(&t, bind(Print, ref(cout), _1)); // WORKS
> for_each_treenode(&t, bind(Print, ref(cout), _1)); // GENERATES ERROR (2)

This is a bit more convoluted. The problem is that for_each_treenode uses
bind(Print, ref(cout), _1) as an argument to bind. The inner bind is
interpreted as a nested bind expression and evaluated. Use either

for_each_treenode(&t, protect(bind(Print, ref(cout), _1))); // should work

or change the bind in for_each_treenode to bind(thisFunc, _1, protect(f)).

protect lives in <boost/bind/protect.hpp>.

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