Can C++11 cannot dynamically call function?

Pages: 12
Say I have a function pointer or std::function object, and I have a variable to hold the return value and a std::vector<boost::any-like container> of parameters to call with, evaluated at runtime. Let's also say I can guarantee the function signature matches the parameter types and the parameter types are limited to pointers, ints, and floats.

Is there any way to, at runtime, call the function with the arguments? The only solution I've seen is inline ASM that pushes the parameters in a loop, but I'm trying to find a pure C++ solution.

Note that this topic is marked as SOLVED. I already have ample proof that this is simply not possible without an external library or inline assembly.
Last edited on
You're talking about using pointers to functions that each take a different number of arguments?

You can play with the C++11 preprocessor to help you along; also you can use the Boost preprocessor macros (like BOOST_PP_REPEAT or BOOST_PP_ENUM_PARAMS), but no matter how you go about coding it you will have to write a switch on argument count to choose between function to activate.
Huh, well it has to be done at runtime. I guess it is fine because I only need to pass 0 to 16 parameters of 32 bits each, but that still seems like it should have a simple way to achieve that.

Thanks anyway!
Last edited on
It doesn't make sense to do this at runtime, since all the functions have to be written at compile time. You might be able to achieve this using variadic templates kind of like how tuple is coded.
It makes complete sense to do this at runtime, if that's what program logic calls for.

since all the functions have to be written at compile time


Let's go a step further: Since all code must be written at compile time, there should be no question which virtual function should be called anywhere, because since we have all the code, we must know what type it's being called for. I think we're on to something. Since we have all this code, I think we should go ahead and eliminate all branching too. I mean, why should the computer have to figure out if a condition is true at runtime? Can't we predetermine it at compile time? We have all the code! =)

I'm sure I'm missing something, but wouldn't the easiest solution be to pass the container of parameters to the function?
If you really want to do this at runtime, you could pass the container of arguments to some wrapper function and just use a large if-else block to select the actual function to call. You would need some way to identify the types in the any-like containers at runtime, and it wouldn't be very fast.

I'm not really seeing any uses for this, though. What situation would force you to take arbitrary permutations of ints, floats, and pointers together? Moreover, why would you need to be able to switch between functions to call based on these permutations? The argument container would have to change in order for this system to be useful at all; otherwise you could just decide which types will be in the container in advance and use a function pointer. Since you have to write all the functions in advance, the permutations that actually bind to functions will be pretty limited as well. It seems like whatever you're trying to accomplish could be more easily done with a feature built into the language.
If I understood correctly, you are trying to accomplish something like Methods [1] or MethodHandles [2] can in Java.

I don't know if C++11 can do it, but for sure there exist some C++ reflection frameworks, e.g. XCppRefl [3]. It probably won't be anywhere near as fast as MethodHandles in Java, but, well at least you can do it ;)


I'm not really seeing any uses for this, though


Thousands of creators of various Java/C#/Python/Ruby/Scala libraries and frameworks would have a good laugh after reading that sentence. Reflection is pretty basic concept and most OO languages provide it.

[1] http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Method.html
[2] http://docs.oracle.com/javase/7/docs/api/java/lang/invoke/MethodHandle.html
[3] http://www.extreme.indiana.edu/reflcpp/
Last edited on
@LB
Will it help if you were to create an array of pointer to functions with the index as follows
function[0] = NULL
function[1] = Function with 1 args
function[2] = Function with 2 args
function[3] = Function with 3 args

and then at runtime you can call function[container.size()]
The problem is mapping the container contents to the function parameters.
So, you mean to say that there will be different functions, each with equal number of arguments but different types and in different order?
None of the solutions mentioned here are applicable. There is already a solution with inline assembly, I was just looking for C++ code that did the same thing.

All the functions, although written at compile time, need slightly more information to describe them that cannot be known at compile time. The idea is to make all this stuff out of the way of whoever uses the SDK so all they do is write the functions and the information describing the functions and not have to worry about tediously creating jump tables or handling the parameter stuff themselves (which is what the previous SDK did and was abandoned for).

I appreciate everyone trying to help, but I guess I should have been more specific.

The idea is to make all this stuff out of the way of whoever uses the SDK so all they do is write the functions and the information describing the functions and not have to worry about tediously creating jump tables or handling the parameter stuff themselves (which is what the previous SDK did and was abandoned for).


This is the exact case for reflection. Just search for reflection libraries for C++.
I don't completely understand your problem but it may lend itself to this static solution using variadic templates.

http://stackoverflow.com/questions/7858817/unpacking-a-tuple-to-call-a-matching-function-pointer

Unless you can load a file with tuple unpacking...
How about,

1. Modify the any container so that it can supply "format string", i.e. a list of variable types in the container in some form (thinking of printf here but could be other than a string type)

2. Use a code generation system to generate the dispatching code based on these "format strings"

I have used
http://nedbatchelder.com/code/cog/
in the past - OK, its not pure C++

put this code generation into the build system
The file may differ at runtime, so any compile time options are, unfortunately, not an option. There are no limits - functions can be added to the file that do not actually exist, in which case a generic handler is called with the old technique. The whole point, however, is to avoid this when possible.
Last edited on
There must be some logic r.e. selecting the function (or generic handler).

Using code generation gives you the maximum flexibility r.e. the logic for function selection. It is hard to see what could be done further.
Its probably because I'm stupid, but I still don't get it... :(
I think you are using the wrong language. This is the kind of "how do I manage memory by myself" question asked on the Java forum... Well, you can to some extent, but it is not practical.

The kind of functionality you request requires some kind of a runtime reflection system, i.e. knowing which objects have which methods, and with what arguments, etc. This information has to be put there by:
1. a compiler
2. some code generation framework,
3. by the programmer.

1. is the way taken by various VMs, but you can't have it in C++ unless you bundle a C++ compiler with your application, or learn how to read non-standard debug information put by the compilers. Not practical. 2. is what reflection libraries for C++ do, but they lose dynamism and flexibility.
Last edited on
Pages: 12