Re: [Boost-users] Boost Lambda Newbie question

Hi, I'm experimenting with the boost lambda library, and I wrote the following code, to go through the vector of numbers and print out statements if the number is greater than 5 or less than 5 int a[]={1,2,3,4,5,6,7,8,9,10,10,9,8,4,7,6,5,4,3,3,1}; std::vector<int> v(a,a+sizeof(a)/sizeof(int)); std::for_each ( v.begin() , v.end() , (if_(_1<5)[ std::cout<<var((boost::format("%d is less than 5\n")%_1).str()) ].else_[ std::cout<<var((boost::format("%d is greater than 5\n")%_1).str()) ]) ); The boost::basic_format::operator % code gives an assertion since the object that is being passed is the placeholder object, and not the current value. Is there a correct way of doing this? Thanks, Haroon

Hi, How can we use the Boost::lambda::bind function to behave the same as the following class some_class{ public: some_class& foo(int x){....} void bar() {...} } some_class a_object; a_object.foo(x).bar(), where the call to some_class::foo returns a reference to the object. I could use Boost.Bind library to accomplish the above as follows boost::bind(&some_class::bar, boost::bind(&some_class::foo, some_functor(), _1)) and I need to do this using boost.lambda. Thanks, Haroon

I managed to solve the problem, as shown in the code below. I had mixed up the Boost.Bind and Boost.Lambda , and totally confused myself. Thanks, Haroon struct some_functor { private: int x_; public: some_functor& foo(int x) { std::cout<<boost::format("Calling %x.%s(%d)\n") % this % __FUNCTION__%x; x_=x; return *this; } some_functor &bar() { std::cout<<boost::format("Calling %x.%s \n")%this%__FUNCTION__; std::cout<<boost::format("%x->x=%d\n")%this%x_; return *this; } }; void test16() { PRINT_FUNCTION_NAME; using namespace boost::lambda; bind(&some_functor::bar, var(bind(&some_functor::foo, var(some_functor()), _1) ) )(boost::cref(1)); int a[] = {1, 2, 3, 4, 5, 6, 7, 8}; std::vector<int> v(a, a+sizeof(a)/sizeof(int)); std::for_each ( v.begin(), v.end() , bind(&some_functor::bar, var(bind(&some_functor::foo, var(some_functor()), _1) ) ) ); } On 11/29/06, Jaakko Järvi <jarvi@cs.tamu.edu> wrote:

On Nov 27, 2006, at 1:14 AM, Haroon Khan wrote:
There is, but it is kind of complicated. Lambda Library gets kind of hairy with more complex lambda expressions: #include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> #include <boost/format.hpp> #include <boost/lambda/if.hpp> #include <boost/lambda/construct.hpp> #include <iostream> using namespace boost::lambda; int main() { int a[]={1,2,3,4,5,6,7,8,9,10,10,9,8,4,7,6,5,4,3,3,1}; std::vector<int> v(a,a+sizeof(a)/sizeof(int)); std::for_each ( v.begin(), v.end(), (if_(_1<5)[ std::cout << ret<boost::format>(bind(constructor<boost::format>(), "%d is less than 5\n") % _1) ].else_[ std::cout << ret<boost::format>(bind(constructor<boost::format>(), "%d is greater than 5\n") % _1) ]) ); } The bind(constructor<boost::format>(), ...) is necessary, because you really need to construct a new format object at every iteration, otherwise the format library throws an exception saying you are trying to pass to many parameters to a format object, more that what is specfied in the format string. the ret<boost::format>(...) is needed to tell lambda what the return type of format's operator% is. All in all, the above lambda is complex enough that writing a function object explicitly might be more justified... at least until we have built-in lambdas, if that ever happens. Cheers, Jaakko
participants (2)
-
Haroon Khan
-
Jaakko Järvi