Boost logo

Boost Users :

Subject: [Boost-users] [lambda] extending lambda functions
From: alfC (alfredo.correa_at_[hidden])
Date: 2009-03-21 14:08:47


For some application I need to sum the complex modulus of a collection
of elements.
In short, I want to be able to replace the lambda expression
_1 + bind(&std::norm<double>, _2)
by something more natural like
_1 + llnorm(_2)

For that I use std::accumulate and the lambda library, like in the
next working example;

#include<boost/lambda/lambda.hpp>
#include<boost/lambda/bind.hpp>
using namespace boost::lambda;
int main(){
  using boost::lambda::_1;
  using boost::lambda::_2;
  std::vector<std::complex<double> > v(3);
  v[0]=1.; v[1]=2.; v[2]=3.;
  double result=std::accumulate(v.begin(), v.end(), double(0.),
                  _1 + bind(&std::norm<double>, _2));
  std::cout<<result<<std::endl;
  return 0;
}

the question is what do I have to do to be able to use the following
syntax instead
                 _1 + llnorm(_2)

where llnorm is something I have to define (globally) somewhere else.
The question is, how do I have to define llnorm to have the desired
effect?
The ideal syntax for me would be to be able to say std::norm(_2) or
std::norm<double>(_2), but that may not be possible due to name
conflict, is it?

I am clueless because I don't even know even what is the (C++) type of
the argument that would be taken by llnorm.

Thanks,
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