I try to transform all elements of a vector v
into their log values with some other arithmetic operations (not in the code).
How can I use Boost.Lambda to achieve that?
As I say, there are some more arithmetic operations, so an expression with Boost.Bind doesn't work for me (too complicated, too long, unreadable).
I don't want to use C++11 lambdas as well. But... would it change anything?
My code is like:
#include <boost/lambda/lambda.hpp>
#include <cmath>
#include <vector>
void testLambda()
{
using namespace boost::lambda;
std::vector<double> v;
v.push_back(1); v.push_back(2); v.push_back(3);
std::transform(v.begin(), v.end(), v.begin(), _1 / 0.5); // works
std::transform(v.begin(), v.end(), v.begin(), log(_1) / 0.5); // produces error
//std::transform(v.begin(), v.end(), v.begin(), std::log(_1) / 0.5); // produces error
//std::transform(v.begin(), v.end(), v.begin(), static_cast<double(*)(double)>(std::log)(_1) / 0.5); // produces error
}
When I try to compile the code, MSVC2010 gives the error:
Error 1 error C2665: 'log' : none of the 3 overloads could convert all the argument types
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\math.h(120): could be 'double log(double)'
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\math.h(527): or 'float log(float)'
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\math.h(575): or 'long double log(long double)'
while trying to match the argument list '(boost::lambda::placeholder1_type)'
Update 1: I don't want to write functors for it, think that I would have to have a dozen of them, what then?
Update 2: I am able to do it with C++11 lambdas, but it's not what I ask for:
std::transform(v.begin(), v.end(), v.begin(), [](const double & x) { return log(x) / 0.5; });