Boost logo

Boost Users :

From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-04-08 14:07:52


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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net