c++ Generate function pointer declaration at run-time? -


is possible create/generate pointer declaration similar to:

void (*foo)(int, float);  bool (*foo)();  char (*foo)(char, int); 

but without knowing type of arguments or return type until run-time.

the function declaration read string specify return , argument type (if any) (if possible) stored in c++ container.

can done @ run-time (not compile-time) ? , c++11 can used if necessary.

i doubt can done in statically typed language c++ if can done approach use. no need code (but it's appreciated) guidance must used.

edit:

after testing several ideas turns out can't achieved (directly) c++. luckily found dyncall library allows me do (indirectly) , on quite large number of platforms.

example function:

double sqrt(double x); 

using dyncall call function:

double r; dccallvm* vm = dcnewcallvm(4096); dcmode(vm, dc_call_c_default); dcreset(vm); dcargdouble(vm, 4.2373); r = dccalldouble(vm, (dcpointer)&sqrt); dcfree(vm); 

strings can used declare structure of function.

c function prototype                                dyncall signature  void f1();                                          ")v" int f2(int, int);                                   "ii)i" long long f3(void*);                                "p)l" void f3(int**);                                     "p)v" double f4(int, bool, char, double, const char*);    "ibcdz)d" 

it depends how many types of arguments handle. technically - can in c++, it's not simple want be.

you use delegate pattern:

class basedelegate(){};  template<typename rett, typename paramt> class delegate: public basedelegate{     public:     rett (*ptr)(paramtt); };  vector<basedelegate*> yourdelegatelist; 

since tagged topic c++11, use std::function, variadic templates , on, make easier. above code example.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -