Boost logo

Boost Users :

Subject: [Boost-users] Two questions on boost::function
From: CR Koudella (ckoudell_at_[hidden])
Date: 2009-11-28 08:15:18


Hi,

thank you very much for your help.

What you suggest works fine, thanks. I have 'studied' the boost lambda library a bit and get the
feeling as it says in the intro to the doc that it is especially meant to write small lambda expressions
for use with the STL. I am not actually that interested in this use and am having a bit of time trying
to get it to do some other things.

Imagine I want to do the following operation [h * (f - g)^2](x), where the x is 'const std::vector<double> &',
all functions return double and h(x) = k1 and g(x) = k2, where k1, k2 = const. f is some non-trivial function. I can make this work according to the method you indicate. I am not entirely sure what the optimal is to build the lambda
expressions for the constant functions h and g, see below. It seems that I need to drive this step through
boost::function, otherwise the code won't compile.

    // some function f given...

    // constant h functor
    const double dh = 4.0;
    bll::constant_type<double>::type lh(bll::constant(dh));
    boost::function<double (const std::vector<double> & vecArg)> bfoh;
    bfoh = lh;
    
    // constant g functor
    const double dg = 0.5;
    bll::constant_type<double>::type lg(bll::constant(dg));
    boost::function<double (const std::vector<double> & vecArg)> bfog;
    bfog = lg;
    
    // construct h * (f - g)^2
    // f - g
    boost::function<double (const std::vector<double> & vecArg)> bfoDF;
    bfoDF = bll::bind(f, bll::_1) - bll::bind(bfog, bll::_1);

    // (f - g)^2
    boost::function<double (const std::vector<double> & vecArg)> bfoSF;
    bfoSF = bll::bind(bfoDF, bll::_1) * bll::bind(bfoDF, bll::_1);

    // h * (f -g)^2
    boost::function<double (const std::vector<double> & vecArg)> bfoWRF;
    bfoWRF = bll::bind(bfoh, bll::_1) * bll::bind(bfoSF, bll::_1);

Other than this, the repetitive use of bll::bind at every stage and generally speaking,
the wordiness of the script makes me wonder whether this is the right thing to use
or if I am using it as I should. I mean h*(f-g)^2 is a pretty simple operation. Any
comments on how to improve the above? In particular on how to build the constant
functors h and g?

Another question I have is, would it be possible to combine functors with different
return types in the same way? Imagine j(x) is now a function that takes in an
std::vector<double> and returns an std::vector<double>. Then can it be scaled
with a function that takes in an std::vector<double>, but returns a double?

BTW, a propos extracting the underlying functor out of a boost::function, yes it
isn't ideal, but you might just need to acess the underlying interface at some
point.

Thanks,
ChrisK

>
                                               



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