#include <algorithm>
#include <iterator>
#include <iostream>
#include <memory>
#include <numeric>
#include <vector>
using namespace std;
struct foo {
int b() const { return _b; }
int a() const { return _a; }
int r() const { return _r; }
const int _b;
const int _a;
const int _r;
};
template <int (foo ::*T)() const>
int func(const int init, const foo* i) { return init + (i->*T)(); }
int main() {
const vector<foo*> foos { new foo{ 0, 1, 2 }, new foo{ 3, 4, 5 }, new foo{ 6, 7, 8 } };
cout << accumulate(cbegin(foos), cend(foos), 0, &func<&foo::r>);
for_each(begin(foos), end(foos), default_delete<foo>());
}
I2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGl0ZXJhdG9yPgojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDxtZW1vcnk+CiNpbmNsdWRlIDxudW1lcmljPgojaW5jbHVkZSA8dmVjdG9yPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnN0cnVjdCBmb28gewogICAgaW50IGIoKSBjb25zdCB7IHJldHVybiBfYjsgfQogICAgaW50IGEoKSBjb25zdCB7IHJldHVybiBfYTsgfQogICAgaW50IHIoKSBjb25zdCB7IHJldHVybiBfcjsgfQogICAgY29uc3QgaW50IF9iOwogICAgY29uc3QgaW50IF9hOwogICAgY29uc3QgaW50IF9yOwp9OwoKdGVtcGxhdGUgPGludCAoZm9vIDo6KlQpKCkgY29uc3Q+CmludCBmdW5jKGNvbnN0IGludCBpbml0LCBjb25zdCBmb28qIGkpIHsgcmV0dXJuIGluaXQgKyAoaS0+KlQpKCk7IH0KCmludCBtYWluKCkgewoJY29uc3QgdmVjdG9yPGZvbyo+IGZvb3MgeyBuZXcgZm9veyAwLCAxLCAyIH0sIG5ldyBmb297IDMsIDQsIDUgfSwgbmV3IGZvb3sgNiwgNywgOCB9IH07CiAKCWNvdXQgPDwgYWNjdW11bGF0ZShjYmVnaW4oZm9vcyksIGNlbmQoZm9vcyksIDAsICZmdW5jPCZmb286OnI+KTsKCQoJZm9yX2VhY2goYmVnaW4oZm9vcyksIGVuZChmb29zKSwgZGVmYXVsdF9kZWxldGU8Zm9vPigpKTsKfQ==