#include <iostream>
#include <functional>
class Fred {
public:
void f() { std::cout << "f" << std::endl; }
void g() { std::cout << "g" << std::endl; }
void h() { std::cout << "h" << std::endl; }
void i() { std::cout << "i" << std::endl; }
};
typedef void (Fred::*FredMemFn)();
FredMemFn a[] = { &Fred::f, &Fred::g, &Fred::h, &Fred::i };
void userCode(Fred& fred, int memFnNum)
{
std::function<void()> fn = std::bind(a[memFnNum], &fred);
fn();
}
int main() {
Fred fred;
userCode(fred, 1);
userCode(fred, 3);
userCode(fred, 0);
userCode(fred, 2);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KCmNsYXNzIEZyZWQgewpwdWJsaWM6CiAgdm9pZCBmKCkgeyBzdGQ6OmNvdXQgPDwgImYiIDw8IHN0ZDo6ZW5kbDsgfQogIHZvaWQgZygpIHsgc3RkOjpjb3V0IDw8ICJnIiA8PCBzdGQ6OmVuZGw7IH0KICB2b2lkIGgoKSB7IHN0ZDo6Y291dCA8PCAiaCIgPDwgc3RkOjplbmRsOyB9CiAgdm9pZCBpKCkgeyBzdGQ6OmNvdXQgPDwgImkiIDw8IHN0ZDo6ZW5kbDsgfQoKfTsKCnR5cGVkZWYgIHZvaWQgKEZyZWQ6OipGcmVkTWVtRm4pKCk7CgpGcmVkTWVtRm4gYVtdID0geyAmRnJlZDo6ZiwgJkZyZWQ6OmcsICZGcmVkOjpoLCAmRnJlZDo6aSB9OwoKdm9pZCB1c2VyQ29kZShGcmVkJiBmcmVkLCBpbnQgbWVtRm5OdW0pCnsKICBzdGQ6OmZ1bmN0aW9uPHZvaWQoKT4gZm4gPSBzdGQ6OmJpbmQoYVttZW1Gbk51bV0sICZmcmVkKTsKICAKICBmbigpOwoKfQoKaW50IG1haW4oKSB7CgkKCUZyZWQgZnJlZDsKCQoJdXNlckNvZGUoZnJlZCwgMSk7Cgl1c2VyQ29kZShmcmVkLCAzKTsKCXVzZXJDb2RlKGZyZWQsIDApOwoJdXNlckNvZGUoZnJlZCwgMik7CgkKCXJldHVybiAwOwp9