Boost logo

Boost Users :

From: Leon Mergen (leon_at_[hidden])
Date: 2008-06-14 08:09:19


I'm trying to convert the following code to a more functional counterpart
using std::for_each:

                BOOST_FOREACH ( std::vector <keyword_matchable
*>::value_type i, haystack->_children ) {
                        std::for_each (needle->_children.begin (),
needle->_children.end (),
(&keyword_matchable::_match_new, this,

As you can see, basically this is a nested foreach loop: for each
haystack->_children, we're looping for each needle->_children, and calling
this->_match_new (haystack_child, needle_child).

Now, while code works, as a sort of personal challenge I tried rewriting
this to a purely std::for_each () based approach; however, I don't quite
understand how I am supposed to use the parent loop's _1 inside a nested
loop; consider this code:

                std::for_each(haystack->_children.begin (),
haystack->_children.end (),
                                      needle->_children.begin (),
needle->_children.end (),
                                      boost::lambda::protect (
                                              boost::lambda::bind (

&keyword_matchable::_match_new, this,

As you can see, i have used the boost::lambda::_1 argument twice; logically,
the same argument is provided twice to that function. Now, I've read
something about using boost::lambda::protect to "prevent argument
substitution to take place", however, I fail to understand how this exactly

Anyone has any idea how I can call the function
keyword_matchable::_match_new with both the current nesting level _1 and the
parent nesting level _1 as arguments ?

Thanks in advance!


Leon Mergen

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