Boost logo

Boost Users :

Subject: [Boost-users] Annoying coding
From: Maxim Koshelev (chuchelo_at_[hidden])
Date: 2009-01-20 08:22:23


Hi again,

I have a problem. Not really big one but annoying.
Often we need to write code like this:

struct A{
  int a;
  std::string b;
};

int a_sum(int i, const A &v){ return v.a + i; };

std::vector<A> v;
//fill this vector somewhere

std::accumulate( v.begin(), v.end(), 0, a_sum );

or something like

int m_sum(int i, const std::pair<int, std::string> p) { return p.first + i; };

std::map <std::string, int> m;

std::accumlate( m.begin(), m.end(), 0, m_sum);

Or in common words we need to write some kind functor which just get
member from element and apply it to another functor.

Of course I can write something like:

template<typename T_, typename V_, V_ T_::*Ptr_>
struct get_mem{
   const V_ &operator()(const T_ &elem){
       return elem->*Ptr_;
   };
};

but may be there is library way to do this?


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