2009/11/24 Peter Foelsche <peter_foelsche@agilent.com>
I've the following template function:


template<typename T>
T exp2(const T T0_d)
{ return exp(0.5*T0_d) + 2*exp(T0_d);
}


I want to define this as a lambda function in place (but don't know how).

It will be used with T=double and T=MyAutomaticDerivativeClass.

#include <cmath>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

int main() {
  using namespace boost::lambda;
  double res = (bind(exp, 0.5 * _1) + 2 * bind(exp, _1))(0.5);
}

If exp is overloaded (and I guess it is, because you are going to pass both double and MyAutomaticDerivativeClass as arguments to exp), you'll need to wrap it in a polymorphic function object.

struct Exp {
  template <class Args> 
  struct sig : boost::tuples::element<1, Args> {};

  template <class T>
  T operator()(T arg) const {
    return exp(arg);
  }
};

int main() {
  using namespace boost::lambda;
  double res = (bind(Exp(), 0.5 * _1) + 2 * bind(Exp(), _1))(0.5);
}

By the way, if you have a choice, I recommend using phoenix instead of lambda. It can do everything lambda can do and even more.

Roman Perepelitsa.