boost.lambda and std::abs(double)
Hi, why does the following code not compile? ---8<--- #include <iostream> #include <boost/array.hpp> #include <boost/lambda/lambda.hpp> #include <algorithm> #include <cmath> int main() { using namespace ::boost::lambda; const boost::array<double, 10> factor = { 1e12, 1e9, 1e6, 1e3, 1, 1e-3, 1e-6, 1e-9, 1e-12, 1e-15 }; boost::array<double, 10> values; std::transform( factor.begin(), factor.end(), values.begin(), std::abs( ret<double>( 1e3 / _1 ) ) ); } --->8--- si_short.cpp: In function 'int main()': si_short.cpp:28: error: no matching function for call to 'abs(const boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::explicit_return_type_action<double>, boost::tuples::tuple<boost::lambda::lambda_functor<boost::lambda::lambda_functor_base<boost::lambda::arithmetic_action<boost::lambda::divide_action>, boost::tuples::tuple<const double, boost::lambda::lambda_functor<boost::lambda::placeholder<1> >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type> > >)' /usr/include/stdlib.h:691: note: candidates are: int abs(int) /usr/include/c++/4.2/cstdlib:143: note: long int std::abs(long int) /usr/include/c++/4.2/cstdlib:174: note: long long int __gnu_cxx::abs(long long int) /usr/include/c++/4.2/cmath:89: note: double std::abs(double) /usr/include/c++/4.2/cmath:93: note: float std::abs(float) /usr/include/c++/4.2/cmath:97: note: long double std::abs(long double) Thanks, Olaf
AMDG Olaf Peter wrote:
Hi,
why does the following code not compile?
---8<--- #include <iostream> #include <boost/array.hpp> #include <boost/lambda/lambda.hpp> #include <algorithm> #include <cmath>
int main() { using namespace ::boost::lambda;
const boost::array<double, 10> factor = { 1e12, 1e9, 1e6, 1e3, 1, 1e-3, 1e-6, 1e-9, 1e-12, 1e-15 };
boost::array<double, 10> values;
std::transform( factor.begin(), factor.end(), values.begin(), std::abs( ret<double>( 1e3 / _1 ) ) );
} --->8---
std::abs is only overloaded for numeric types. A lambda function is not a numeric type nor is it convertible to a numeric type. Thus, there is no matching overload. There is no lazy version of std::abs provided with lambda, so the easiest way is to use lambda::bind #include <iostream> #include <boost/array.hpp> #include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> #include <algorithm> #include <cmath> int main() { using namespace ::boost::lambda; const boost::array<double, 10> factor = { 1e12, 1e9, 1e6, 1e3, 1, 1e-3, 1e-6, 1e-9, 1e-12, 1e-15 }; boost::array<double, 10> values; std::transform( factor.begin(), factor.end(), values.begin(), bind(static_cast<double(*)(double)>(&std::abs), 1e3 / _1 ) ); } In Christ, Steven Watanabe
participants (2)
-
Olaf Peter
-
Steven Watanabe