#include <functional>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::for_each;
using std::unary_function;
using std::vector;
template <class Type> class basic_accumulator {
public:
basic_accumulator(void) { m_typ = 0; };
bool accumulate(Type i_typ) { m_typ += i_typ; };
Type results(void) { return m_typ; };
protected:
Type m_typ;
};
typedef basic_accumulator<int> accumulator;
template <class Result, class Type, class Param>
class far_mem_fun_t : public unary_function<Param,Result> {
public:
explicit far_mem_fun_t(Type &ir_typ, Result (Type::*i_pmf)(Param)) {
m_ptyp = &ir_typ;
m_pmf = i_pmf;
};
Result operator()(Param i_prm) { return (m_ptyp->*(m_pmf))(i_prm); };
protected:
Type *m_ptyp;
Result (Type::*m_pmf)(Param);
};
template <class Result, class Type, class Param>
far_mem_fun_t<Result,Type,Param> far_mem_fun(Type &ir_typ, Result (Type::*i_pmf)
(Param) ) {
return far_mem_fun_t<Result,Type,Param>(ir_typ,i_pmf);
}
int main() {
accumulator a;
vector<int> v;
v.push_back(1);
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(5);
v.push_back(8);
for_each(v.begin(),v.end(),far_mem_fun(a,&accumulator::accumulate));
cout << a.results() << endl;
return 0;
}