Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ function call with the parameters unknown at compile time

Tags:

c++

As weird as it sounds, I need to call a function in C++, for which I do not know the signature. Basically, I want to let the user specify a list of arguments, and argument types, and then attempt to call a given function with these arguments. I would then like to know if the function call worked or not, and if it (miraculously) did, what is the return value (provided that the return type is not void). Is there any way to achieve that? Thanks.

like image 549
Emil Avatar asked Jan 22 '10 21:01

Emil


People also ask

Is parameters are mandatory in function call?

The parameters are the variables in a function, whereas the arguments are the values passed to the parameters when calling the function. The parameters can be mandatory or optional.

Does c have pass by reference?

There is no pass-by-reference in C.

What is used for a function to execute at compile time?

In computing, compile-time function execution (or compile time function evaluation, or general constant expressions) is the ability of a compiler, that would normally compile a function to machine code and execute it at run time, to execute the function at compile time.


1 Answers

Let your functions take a list of variant data types, e.g. std::vector<boost::any> and have them return a bool indicating success or throw exceptions on failure.

If you are ok with registering the exposed functions at compile time, you don't even have to place that restriction on your functions and you can instead generate glue code for the neccessary conversions.

like image 71
Georg Fritzsche Avatar answered Oct 05 '22 02:10

Georg Fritzsche