Hello,

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 (),
                                       boost::lambda::bind (&keyword_matchable::_match_new, this,
                                                            i, boost::lambda::_1));                       
                }

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 (),
                              boost::lambda::bind(
                                      boost::lambda::ll::for_each(),
                                      needle->_children.begin (), needle->_children.end (),
                                      boost::lambda::protect (
                                              boost::lambda::bind (
                                                      &keyword_matchable::_match_new, this,
                                                      boost::lambda::_1,
                                                      boost::lambda::_1))));


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 works.

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!

Regards,

Leon Mergen