I am trying to use boost::lambda::bind() to define a predicate that I pass to the find_if algorithm in Boost.Range. In particular, I want to find a vector of structures to find the first record where a particular member has the specified value. My example is as follows:
#include <boost/lambda/bind.hpp> #include <boost/range/algorithm/find_if.hpp> #include <vector> using namespace std; using namespace boost; using namespace boost::lambda; struct foo { string s; int x; }; int main() { // create list and add a couple entries vector<foo> fooList; foo f1 = {"abc", 1}; foo f2 = {"def", 2}; fooList.push_back(f1); fooList.push_back(f2); // search for a value with the desired member // fails with a compile error! range_iterator<vector<foo> > it = find_if(fooList, boost::lambda::bind(&foo::s, _1) == string("abc")); return 0; }
When I try to compile this (in gcc 4.7.2 section), I get a typical fingerprint of template creation errors, which indicates the absence of operator== found, compatible with the type returned by bind() and a const char [] . I tried this with other types as well, for example int , with the same result.
I miss a little information about using bind() , but I don't see it; it seems that such work should be based on documentation. Am I wrong there?
Edit: Here is the first part of the compiler output:
test.cc:24:92: error: no match for 'operator==' in 'boost::lambda::bind(const Arg1&, const Arg2&) [with Arg1 = std::basic_string<char> foo::*; Arg2 = boost::lambda::lambda_functor<boost::lambda::placeholder<1> >; typename boost::lambda::detail::bind_tuple_mapper<const Arg1, const Arg2>::type = boost::tuples::tuple<std::basic_string<char> foo::* const, const boost::lambda::lambda_functor<boost::lambda::placeholder<1> >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>]((* & boost::lambda::{anonymous}::_1)) == "abc"'
source share