fork(1) download
  1. #include <algorithm>
  2. #include <iterator>
  3. #include <iostream>
  4. #include <memory>
  5. #include <numeric>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. struct foo {
  11. int b() const { return _b; }
  12. int a() const { return _a; }
  13. int r() const { return _r; }
  14. const int _b;
  15. const int _a;
  16. const int _r;
  17. };
  18.  
  19. template <int (foo::*T)() const>
  20. auto func = [](const auto init, const auto i){ return init + (i->*T)(); };
  21.  
  22. int main() {
  23. const vector<foo*> foos { new foo{ 0, 1, 2 }, new foo{ 3, 4, 5 }, new foo{ 6, 7, 8 } };
  24.  
  25. cout << accumulate(cbegin(foos), cend(foos), 0, func<&foo::r>);
  26.  
  27. for_each(begin(foos), end(foos), default_delete<foo>());
  28. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
15