[bind] [lambda] and [function] in combination with each other
I'm using a map of comparing functions, created with Boost.Lambda and Boost.Function. These are of type boost::function<bool(double, double)> f = ( _1 <= _2 ) Now I need one of these functions bound to a specific value to send this function object as a callback function. Things are compiling fine, but the result is wrong. I've boiled it down to a minimal example: Any hints? TIA Chris --------8<--------------8<------------ #include <iostream> #include <map> #include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> #include <boost/function.hpp> #include <boost/bind.hpp> typedef boost::function<bool(const double& ,const double& )> CMP_FNC_DBL_DBL; typedef boost::function<bool(const double& )> CMP_FNC_DBL; typedef std::map<std::string, CMP_FNC_DBL_DBL> FUNCTION_MAP; int main() { FUNCTION_MAP fm; fm["=="] = (boost::lambda::_1 == boost::lambda::_2); fm["!="] = (boost::lambda::_1 != boost::lambda::_2); //... CMP_FNC_DBL_DBL fdd = fm["=="]; std::cout << fdd(11., 10.) << std::endl; //works //Getting a function object via bind CMP_FNC_DBL_DBL fdd2 = boost::bind(&FUNCTION_MAP::operator[] , fm, std::string("==")); std::cout << fdd2(11., 10.) << std::endl; //works not //that is what I really need: a CMP_FNC_DBL_DBL bound to a constant value CMP_FNC_DBL f = boost::bind(boost::bind(&FUNCTION_MAP::operator[] , fm, std::string("==")), 4.); //doesnt compile std::cout << f(3) << std::endl; //works not } --------8<--------------8<------------ result: 0 1 1
Christoph Borgolte wrote: [...]
typedef boost::function<bool(const double& ,const double& )> CMP_FNC_DBL_DBL; typedef boost::function<bool(const double& )> CMP_FNC_DBL; typedef std::map<std::string, CMP_FNC_DBL_DBL> FUNCTION_MAP;
int main() { FUNCTION_MAP fm; fm["=="] = (boost::lambda::_1 == boost::lambda::_2); fm["!="] = (boost::lambda::_1 != boost::lambda::_2); //...
CMP_FNC_DBL_DBL fdd = fm["=="]; std::cout << fdd(11., 10.) << std::endl; //works
//Getting a function object via bind CMP_FNC_DBL_DBL fdd2 = boost::bind(&FUNCTION_MAP::operator[] , fm, std::string("=="));
This gives you a fdd2 that returns fm["=="]. It works because fm["=="] is convertible to bool, but doesn't really make much sense.
//that is what I really need: a CMP_FNC_DBL_DBL bound to a constant value
CMP_FNC_DBL fdd3 = bind( fdd, _1, 4.0 ); // ?
CMP_FNC_DBL fdd3 = bind( fdd, _1, 4.0 ); // ?
Thanks a lot! That's what I first tried, but I was so stupid to use the wrong variable (a bool) to be bound and I wasn't able to interpret the compiler warnings. So I searched at the wrong end. regards Chris
participants (2)
-
Christoph Borgolte
-
Peter Dimov