How can I std :: sqrt (boost :: lambda :: placeholder1_type)?
How std::sqrt()
on boost::lambda::placeholder1_type
?
The following program compiles and executes fine. Anyway, it manages to convert boost::lambda::_1
to double and multiply it by 5.0.
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <cmath>
int main() {
std::array<double, 6> a = {1, 4, 9, 16, 25, 36};
std::for_each(a.begin(), a.end(),
std::cout << boost::lambda::_1 * 5.0 << " "
);
}
However, if I replace the line std::cout
with
std::cout << std::sqrt(boost::lambda::_1) << " "
the compiler (g ++) says
no known conversion for argument 1 from 'Boost :: lambda :: placeholder1_type {aka const boost :: lambda :: lambda_functor>} for "Double
So how can I take the square root of boost::lambda::_1
in this std :: for_each loop?
source to share
You need to postpone the call sqrt
. To achieve this, you must use
bind an expression .
Note. To select the correct overload, sqrt
you need to use a throw.
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <iostream>
#include <cmath>
int main()
{
std::array<double, 6> a = {1, 4, 9, 16, 25, 36};
std::for_each(a.begin(), a.end(),
std::cout << boost::lambda::bind(static_cast<double(*)(double)>(std::sqrt), boost::lambda::_1) << " "
);
}
Output:
1 2 3 4 5 6
source to share