[lambda] Binding to templated functions

The Boost.Lambda library continues to delight and frustrate me! I have this code sequence, which is intended to determine whether a predicate is true for all objects pointed to by the second member of all of vector of pairs, #include <utility> #include <vector> #include <algorithm> #include "boost/lambda/lambda.hpp" #include "boost/lambda/bind.hpp" struct S { bool predOfS( ); }; int main( ) { using namespace std; using namespace boost :: lambda; typedef pair<int, S *> Pair; vector<Pair> pairs; bool trueForAll = ( find_if( _1, _2, _3 ) == _2 )( pairs.begin( ), pairs.end( ), ! bind( & S :: predOfS, bind( & Pair :: second, _1 ) ) ); } The last line gives me compile errors. Two things: - I think I need to specicy the template parameters to find_if, but I'm not sure quite what they are! - The error message reckons the last arg to find_if is placeholder<4>, not placeholder<3>, why? Or maybe there's completely different way to do this kind of thing? Thanks, Rob.

Robert Jones wrote:
typedef pair<int, S *> Pair; vector<Pair> pairs; bool trueForAll = ( find_if( _1, _2, _3 ) == _2 )( pairs.begin( ), pairs.end( ), ! bind( & S :: predOfS, bind( & Pair :: second, _1 ) ) ); }
The last line gives me compile errors.
Two things: - I think I need to specicy the template parameters to find_if, but I'm not sure quite what they are!
Why are you even doing this? find_if(pairs.begin(), pairs.end(), your_lambda_expression) is sufficient. Otherwise, it is more practical to use a lazy function. Phoenix is better for that, and it actually already provides lazy versions of standard algorithms.
- The error message reckons the last arg to find_if is placeholder<4>, not placeholder<3>, why?
Because _1 is placeholder<0>.

On Tue, Jul 22, 2008 at 12:28 PM, Mathias Gaunard < mathias.gaunard@ens-lyon.org> wrote:
Robert Jones wrote:
typedef pair<int, S *> Pair; vector<Pair> pairs; bool trueForAll = ( find_if( _1, _2, _3 ) == _2 )( pairs.begin( ), pairs.end( ), ! bind( & S :: predOfS, bind( & Pair :: second, _1 ) ) ); }
The last line gives me compile errors.
Two things: - I think I need to specicy the template parameters to find_if, but I'm not sure quite what they are!
Why are you even doing this? find_if(pairs.begin(), pairs.end(), your_lambda_expression) is sufficient.
Otherwise, it is more practical to use a lazy function. Phoenix is better for that, and it actually already provides lazy versions of standard algorithms.
- The error message reckons the last arg to find_if is placeholder<4>, not placeholder<3>, why?
Because _1 is placeholder<0>.
Hi Mathias I can entirely understand your horror at this code, and indeed it is highly contrived to be about the simplest thing I can construct which reproduces the symptoms produced from the real code. The essential feature, in the terms of my example code, is that I only want to evaluate the end( ) method once, and avoid placing it in a named variable. It seems Phoenix may be the answer to my needs, so many thanks for that tip. Regards, Rob.
participants (2)
-
Mathias Gaunard
-
Robert Jones