How to do a simple C++ generic callback? -


say have following code:

class { public:     a() {}      int f(void*, void*)     {         return 0;     } };  template <typename t> class f { public:     f(int(t::*f)(void*,void*))     {         this->f = f;     }      int(t::*f)(void*,void*);      int call(t& t,void* a,void* b)     {         return (t.*f)(a,b);     } };  a; f<a> f(&a::f); f.call(a, 0, 0); 

well works can call function how example have array of these without knowing type?

i able call class f function callback. i'd use static or c function , done wanted experiment calling c++ member function.

think of i'm trying delegates in c#. i've seen sophisticated implementations in boost , other places want bare minimum, no copying, creation, deletion etc required simple callback can call.

is pipedream? advice appreciated.

i think bare minimum implementation following:

std::vector<std::function<bool(int, int)>> my_delegate; 

you can add many different types of callable items vector. such as:

bool mycompare(int a, int b) {     return < b; }  struct predicate {     bool operator()(int a, int b)     {         return < b;     } };  struct myclass {     bool function(int a, int b)     {         return < b;     } };  my_delegate.push_back(predicate()); my_delegate.push_back(mycompare); my_delegate.push_back([](int a, int b){return < b;}); myclass c; my_delegate.push_back(std::bind(std::bind(&myclass::function, &c, std::placeholders::_1, std::placeholders::_2)); 

calling functions in delegate matter of looping through them , calling each in turn.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -