C ++ function call with unknown parameters at compile time

Oddly enough, I need to call a function in C ++, for which I do not know the signature. Basically, I want the user to specify a list of arguments and types of arguments, and then try to call this function with these arguments. Then I would like to know if the function call works or not, and if it is (miraculously), then what is the return value (provided that the return type is not invalid). Is there any way to achieve this? Thanks.

+6
c ++
source share
8 answers

Let your functions take a list of data type options, for example. std::vector<boost::any> and return them a bool indicating success, or throw an exception on error.

If you are fine with registering public functions at compile time, you don’t even need to set this restriction on your functions, and instead you can generate glue code for the necessary transformations.

+5
source share

Of course you can do this at runtime. Gdb firstly does this. But this requires a lot of work and understanding of the target environment. I suspect that there may be a better way to do what you want.

+3
source share

It doesn't sound too weird, but you still can't do it in C ++. C ++ function signatures are checked at compile time. The best you can do is pass a vector or similar container containing your functional parameters for the function. Even then, the types (or at least the base type, if you use inheritance) had to be known at compile time.

0
source share

If you have access to the C ++ compiler at run time, you can write the postulated call to a file, compile it, and then link it dynamically as a DLL and try to run. If the argument types are incorrect or the function does not exist, the runtime of the DLL is not executed; otherwise the function will be called and you will get the return value.

0
source share

Well, if you are flexible about the mechanism, you can do this with dynamic sending.

What you do is create an abstract base class that has a run method, returning a return value. Then you have an engine that stores a list of pointers to objects of this (abstract) base class that clients can provide you through some kind of registration call.

Clients write their own "run it" implementations that can do whatever they need, as long as they correctly populate the return value for your engine when they are executed. Their "user parameters" are implemented as additional members for their version of the class, which can be populated when they are created or by a separate call.

This gives you what you need to call their subroutine, run it and check the result, not knowing what their user parameters are or what their program does exactly.

0
source share

This is a pointless question, because if the user (who calls from c-code) does not know the types of arguments at compile time, he will not be able to provide arguments of the correct type, even if it were possible to construct a dynamically typed function pointer.

Even the gdb example, expressed in another answer, makes the assumption that the user has variables of a corresponding and known type at the dial peer.

Since this is true, I conclude that the user knows the correct types of the argument, and then the question arises of how to create a function at compile time that takes a set of arguments of a known type.

This is simply achieved: if the user has an int and two float and hopes to get an int in return and wants to call the proposed swizzle function, the type of which should be int (IIIF*)(int, int, float) , then just declare a pointer of this type, use ldload . to get the swizzle function and call it.

However, there are some special cases that can be achieved, for example, if all arguments are pointers.

Note. Known at compile time includes such cases as patterns, the values ​​of which are derived at compile time from explicit or implicit type information at the time the instance and template were created.

0
source share

How to use rvalue links in C ++ ox?

 template<typename Function> void FunCall( Function&& f) { f(); } void Fun_Int(int x) { using std::cout; using std::endl; cout << "I''m Fun_Int with " << x << endl; } void Fun_Str(const string& str1) { using std::cout; using std::endl; cout << "I'm Fun_Str with " << str1 << endl; } void RvaluesDemo() { for (int i = 0; i < 5; i++) { FunCall([&] { Fun_Int(i); }); } string str = "ABCDEF"; for (int i = 0; i < str.size(); i++) { ostringstream os; os << str[i]; FunCall([&] { Fun_Str(os.str()); }); } } 
0
source share

It sounds like a sitter to incorporate a dynamically typed scripting language into your application. Insert something like Lua or Python, make the functions you want to use for the scripting language. Or, if you are trying to implement such a thing, look at Boost :: Python for a precedent on how to do this.

-one
source share

All Articles