
Hello! I am new to the boost lambda library so this might be a naive question. Here goes. This is what I want to do: class Foo { public: int bar() { return 42; } }; int main() { std::vector<Foo> v; v.push_back(Foo()); count_if(v.begin(), v.end(), boost::bind(&Foo::bar, _1) == 42); return 0; } However, I get a comilation error when trying to compile this with g++ version 3.3.2: test.cpp: In function `int main()': test.cpp:17: error: no match for 'operator==' in '`empty_class_expr' not test.cpp:17: sorry, unimplemented: supported by dump_expr test.cpp:17: sorry, unimplemented: boost::bind(R (T::*)(), A1) [with R = int, T = Foo, A1 = boost::arg<1>]((<unnamed>::_1, <expression error>)) == 42' To me it seems that this is something that I should be able to do with the BLL. If someone could point me in the right direction here I would be really greatful! Thanks in advance! Regards, Mattias

Hello Mattias, Wednesday, July 28, 2004, 6:19:21 PM, you wrote: MB> Hello! MB> I am new to the boost lambda library so this might be a naive question. MB> Here goes. This is what I want to do: MB> class Foo { MB> public: MB> int bar() { return 42; } MB> }; MB> int main() MB> { MB> std::vector<Foo> v; MB> v.push_back(Foo()); MB> count_if(v.begin(), v.end(), boost::bind(&Foo::bar, _1) == 42); MB> return 0; MB> } You may try something like this: #include <vector> #include <boost/bind.hpp> #include <functional> class Foo { public: int bar() { return 42; } }; int main(int argc, char* argv[]) { std::vector<Foo> v; v.push_back(Foo()); size_t c = count_if( v.begin(), v.end(), boost::bind( std::equal_to<int>(), boost::bind(Foo::bar, _1), 42 ) ); return 0; } This compiles ok on MS VC7.1, but it doesn't use lambda, only bind. -- Best regards, Владимир mailto:vkrasovsky@yandex.ru

On Wed, 28 Jul 2004 16:19:21 +0200, Mattias Brändström wrote:
To me it seems that this is something that I should be able to do with the BLL. If someone could point me in the right direction here I would be really greatful!
Hi, You're almost there. The problem is that you're using boost::bind instead of boost::lambda::bind. I think that boost::bind is more portable, but it doesn't support lambda expressions. Here's a working version of your code: #include <algorithm> #include <vector> #include <boost/lambda/lambda.hpp> // needed for lambda operators #include <boost/lambda/bind.hpp> // not boost/bind.hpp class Foo { public: int bar() { return 42; } }; int main() { std::vector<Foo> v; v.push_back(Foo()); using namespace boost::lambda; std::count_if(v.begin(), v.end(), bind(&Foo::bar, _1) == 42); return 0; } Daniel
participants (3)
-
Daniel James
-
Mattias Brändström
-
Владимир Красовский