Boost logo

Boost Users :

Subject: [Boost-users] [lambda] make lambda play well with std::complex, imag, real, abs, etc.
From: alfC (alfredo.correa_at_[hidden])
Date: 2010-06-02 17:01:59


Hi, I need some complex arithmetic in lambda expressions, for that I
implemented this to make some std::complex functions work with
Boost.Lambda.

namespace boost{
namespace lambda{
#define BOOST_LAMBDA_COMPLEX_IMPL(function_name) \
template<typename Arg> \
lambda_functor<lambda_functor_base< \
        action<2, function_action<2> >, \
        boost::tuples::tuple< \
                const double& (* const)(const std::complex<double>&), \
                const lambda_functor<Arg> \
> \
> > function_name(const lambda_functor<Arg>& f){ \
        return boost::lambda::bind(static_cast<double const&(*)
(std::complex<double> const&)>(&std::function_name<double>), f); \
}
BOOST_LAMBDA_COMPLEX_IMPL(real)
BOOST_LAMBDA_COMPLEX_IMPL(imag)
BOOST_LAMBDA_COMPLEX_IMPL(abs)
BOOST_LAMBDA_COMPLEX_IMPL(norm)
}
}

use as
boost::function<double(complex)> f = imag(_1) + real(_1) +
abs(_1+2.);

I have two questions about it:
1) Is this something that is implemented in some other way already?

2) it seem imposible to generalized to other template overloads of
imag<T>, real<T> (i.e. something that can be used for std::complex<T>)
because the template parameter Arg doesn't have information about the
actual type. is it so?

(comments on the code and alternatives also accepted)
Thank you,
Alfredo


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