Re: [Boost-users] A question about boost::lambda::if_ behavior

Hello Tom, -----Original Message----- From: tom gee [mailto:rockonedge@gmail.com] Subject: [Boost-users] A question about boost::lambda::if_ behavior
Can anyone please explain this code: [snip]
The problem is that the expression std::cout << constant("Bingo:<") is evaluated immediately. Why? Because it's not a lambda expression. You have two options that will help you turn your if-branch into a lambda expression; either use boost::lambda::var or boost::lambda::constant. Using boost::lambda::var: std::for_each(setFinalVal.begin(),setFinalVal.end(), (std::cout << _1 << " ", if_(_1 < 24.000001 && _1 > 23.999999) [var(std::cout) << "Bingo:<" << _1 << ">"])); Using boost::lambda::constant: std::for_each(setFinalVal.begin(),setFinalVal.end(), (std::cout << _1 << " ", if_(_1 < 24.000001 && _1 > 23.999999) [std::cout << constant("Bingo:<") << _1 << ">"])); Cheers, Bjorn Karlsson

Hello Tom,
-----Original Message----- From: tom gee [mailto:rockonedge@gmail.com] Subject: [Boost-users] A question about boost::lambda::if_ behavior
Can anyone please explain this code:
[snip]
The problem is that the expression std::cout << constant("Bingo:<") is evaluated immediately. Why? Because it's not a lambda expression. You have two options that will help you turn your if-branch into a lambda expression; either use boost::lambda::var or boost::lambda::constant.
Using boost::lambda::var:
std::for_each(setFinalVal.begin(),setFinalVal.end(), (std::cout << _1 << " ", if_(_1 < 24.000001 && _1 > 23.999999) [var(std::cout) << "Bingo:<" << _1 << ">"]));
Using boost::lambda::constant:
std::for_each(setFinalVal.begin(),setFinalVal.end(), (std::cout << _1 << " ", if_(_1 < 24.000001 && _1 > 23.999999) [std::cout << constant("Bingo:<") << _1 << ">"]));
Cheers, Bjorn Karlsson constant should be better (lower overhead, conceptually closer), but no biggy. I beleive this is the No.1 FAQ, is it not? (PS: why does not the lambda docs say in the function call overload
Bjorn.Karlsson@readsoft.com wrote: section to use BLL's bind? It took me AGES to figure out how to do that!)
participants (2)
-
Bjorn.Karlsson@readsoft.com
-
Simon Buchan