Peter Dimov wrote:
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
.
I think, Mr. Dimov, if you could document 'protect' and 'apply' a little better in relation to boost::bind and give more exmaples of their usage, it would be helpful to many programmers running into problems such as the current one has. In particular, explaining better exactly what is meant by 'evaluated' would be helpful in the documentation to nested binds. If I am too picky asking for a clearer and slower explanation of these things in the documentation, then it is clearly my fault that I find some of them hard to understand as written.