Boost logo

Boost Users :

From: Christoph Borgolte (cb_at_[hidden])
Date: 2007-03-07 04:03:16


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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net