#include <functional>
#include <iostream>
#include <string>
struct functor_with_string_data
{
std::string s ;
functor_with_string_data( const std::string & str ) : s(str) {}
void operator()() { std::cout << s << '\n' ; }
};
struct functor_with_int_data
{
int data ;
functor_with_int_data( int num ) : data(num) {}
void operator()() {std::cout << data << '\n' ;}
};
class myclass
{
public:
myclass( std::function<void()> f = nullptr ) : _func(f) {}
void setCallback( std::function<void()> f ) { _func = f ; }
void doCallback() const { _func(); }
private:
std::function<void()> _func ;
};
int main()
{
myclass obj ;
obj.setCallback(functor_with_string_data("Hello, world!")) ;
obj.doCallback() ;
obj.setCallback(functor_with_int_data(42)) ;
obj.doCallback() ;
}
I2luY2x1ZGUgPGZ1bmN0aW9uYWw+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHN0cmluZz4KCnN0cnVjdCBmdW5jdG9yX3dpdGhfc3RyaW5nX2RhdGEKewogICAgc3RkOjpzdHJpbmcgcyA7CgogICAgZnVuY3Rvcl93aXRoX3N0cmluZ19kYXRhKCBjb25zdCBzdGQ6OnN0cmluZyAmIHN0ciApIDogcyhzdHIpIHt9CgogICAgdm9pZCBvcGVyYXRvcigpKCkgeyBzdGQ6OmNvdXQgPDwgcyA8PCAnXG4nIDsgfQp9OwoKc3RydWN0IGZ1bmN0b3Jfd2l0aF9pbnRfZGF0YQp7CiAgICBpbnQgZGF0YSA7CgogICAgZnVuY3Rvcl93aXRoX2ludF9kYXRhKCBpbnQgbnVtICkgOiBkYXRhKG51bSkge30KCiAgICB2b2lkIG9wZXJhdG9yKCkoKSB7c3RkOjpjb3V0IDw8IGRhdGEgPDwgJ1xuJyA7fQp9OwoKCmNsYXNzIG15Y2xhc3MKewpwdWJsaWM6CiAgICBteWNsYXNzKCBzdGQ6OmZ1bmN0aW9uPHZvaWQoKT4gZiA9IG51bGxwdHIgKSA6IF9mdW5jKGYpIHt9CiAgICB2b2lkIHNldENhbGxiYWNrKCBzdGQ6OmZ1bmN0aW9uPHZvaWQoKT4gZiApIHsg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