Two questions on boost::function
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
On Sat, Nov 28, 2009 at 6:15 AM, CR Koudella
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
bfoh; bfoh = lh; // constant g functor const double dg = 0.5; bll::constant_type<double>::type lg(bll::constant(dg)); boost::function
bfog; bfog = lg; // construct h * (f - g)^2 // f - g boost::function
bfoDF; bfoDF = bll::bind(f, bll::_1) - bll::bind(bfog, bll::_1); // (f - g)^2 boost::function
bfoSF; bfoSF = bll::bind(bfoDF, bll::_1) * bll::bind(bfoDF, bll::_1); // h * (f -g)^2 boost::function
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.
For note, that is all easier with Boost.Phoenix, which supersedes Boost.Lambda, has all of its functionality and a great deal more.
participants (2)
-
CR Koudella
-
OvermindDL1