STL summation can be done using std::accumulate
#include <functional> accumulate(v.begin(), v.end(), 0, bind(plus<int>(), _1, bind(&ABCD::a, _2)))
If you want this to be more general, you can take the tr1 :: function for the member you want to bind:
int sum_over_vec(const vector<ABCD>& v, const tr1::function<int (const ABCD&)>& member) { return accumulate(v.begin(), v.end(), 0, bind(plus<int>(), _1, bind(member, _2))); };
Another way to do this, instead of putting your logic in a functor, is to put the logic in an iterator using the boost :: transform iterator:
tr1::function<int (const ABCD&)> member(bind(&ABCD::a, _1)); accumulate(make_transform_iterator(v.begin(), member), make_transform_iterator(v.end(), member), 0);
EDITED TO ADD: C ++ 11 lambda syntax
This becomes somewhat clearer with lambdas C ++ 11 (although, unfortunately, no shorter):
accumulate(v.begin(), v.end(), 0, [](int sum, const ABCD& curr) { return sum + curr.a });
and
int sum_over_vec(const vector<ABCD>& v, const std::function<int (const ABCD&)>& member) { return accumulate(v.begin(), v.end(), 0, [&](int sum, const ABCD& curr) { return sum + member(curr}); }); };
Using: