#include <functional>
#include <iostream>
class Test
{
public:
void blah() { std::cout << "BLAH!" << std::endl; }
};
class Bim
{
public:
void operator()(){ std::cout << "BIM!" << std::endl; }
};
void boum() { std::cout << "BOUM!" << std::endl; }
int main()
{
// store the member function of an object:
Test test;
std::function< void() > callback = std::bind( &Test::blah, test );
callback();
// store a callable object (by copy)
callback = Bim{};
callback();
// store the address of a static funtion
callback = &boum;
callback();
// store a copy of a lambda (that is a callable object)
callback = [&]{ test.blah(); }; // might be clearer than calling std::bind()
callback();
}
I2luY2x1ZGUgPGZ1bmN0aW9uYWw+CiNpbmNsdWRlIDxpb3N0cmVhbT4KCgogICAgY2xhc3MgVGVzdAogICAgewogICAgcHVibGljOgogICAgICAgICAgdm9pZCBibGFoKCkgeyBzdGQ6OmNvdXQgPDwgIkJMQUghIiA8PCBzdGQ6OmVuZGw7IH0KICAgIH07CgogICAgY2xhc3MgQmltCiAgICB7CiAgICBwdWJsaWM6CiAgICAgICAgICB2b2lkIG9wZXJhdG9yKCkoKXsgc3RkOjpjb3V0IDw8ICJCSU0hIiA8PCBzdGQ6OmVuZGw7IH0KICAgIH07CgogICAgdm9pZCBib3VtKCkgeyBzdGQ6OmNvdXQgPDwgIkJPVU0hIiA8PCBzdGQ6OmVuZGw7IH0KCgppbnQgbWFpbigpCnsKICAgIC8vIHN0b3JlIHRoZSBtZW1iZXIgZnVuY3Rpb24gb2YgYW4gb2JqZWN0OgogICAgVGVzdCB0ZXN0OyAgCiAgICBzdGQ6OmZ1bmN0aW9uPCB2b2lkKCkgPiBjYWxsYmFjayA9IHN0ZDo6YmluZCggJlRlc3Q6OmJsYWgsIHRlc3QgKTsKICAgIGNhbGxiYWNrKCk7CgogICAgLy8gc3RvcmUgYSBjYWxsYWJsZSBvYmplY3QgKGJ5IGNvcHkpCiAgICBjYWxsYmFjayA9IEJpbXt9OwogICAgY2FsbGJhY2soKTsKCiAgICAvLyBzdG9yZSB0aGUgYWRkcmVzcyBvZiBhIHN0YXRpYyBmdW50aW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