I am working on an injection filter function to go via trafic coming to mine ESP8266 and the dispatcher function then decides where to pass the data. Some of the functions are variadic and I need to output different number of variables to them dependent on a situation. How can I output or call a function and send a variable number of variables if I can not edit the end function?
for example if target function is
int foo(int a, ...)
I need to be able to send between 1 and 13 variables to it
The problem is the calling site, not the procesing
You can use an array plus the number of things stored in your array as arguments. All arguments need to be of same type then...
Or you can make a function that takes 13 parameters and add defaults for the ones you do not need for the call with least parameters.
You then simply call the function with more or less parameters. Search for 'function overloading'...
Hello zlodej
Design a data structure with n members and call foo with a reference to this data structure.
Have a nice day and enjoy coding in C++.
Дайте миру шанс!
Thanks for quick reply, but mine point is that I can not edit the foo function as it would break each time its updated, its from external library and they would each require editing each time they are updated (plus they are above mine scope of understanding at this point). Instead I need to format the call of the function to give a different number of variables on a different occasions. As I understand passing a list actually passes a single pointer.
What does the header of this library function look like? Is this library function already overloaded? Can it receive varying numbers of arguments?
Maybe you can write a wrapper function around this library function?
First off, your flawed terminology is confusing. You don't "output" variables to a function, you pass arguments to it.
You say "some of the functions are variadic". Does that mean they're already written to accept a variable number of arguments by using either C++ Templates or the C-style variadic macros? If so, then you don't need to do anything, just call the functions with the desired number of arguments. If they're not already written that way, then they need to be modified and recompiled. You just can't force it to work from the calling function.
The dispatcher function I am writing is meant as a wrapper for some other functions.
We are talking about multiple functions which like foo above that accept variable number of arguments already. These functions do not accept lists, except one that can have it only as a first argument (and still require further variables. At this stage I have something like:
switch(var_num) {
case 1:
foo(a, b[0]);
break;
case 2:
foo(a, b[0],b[1]);
break;
...
case 10:
foo(a, b[0],b[1],b[2],b[3],b[4],b[5],b[6],b[7],b[8],b[9]);
break;
For each function
question is if there is a cleaner way of calling the function that already is variadic and the number of variables is decided at a time of calling the function and not at the time of programming.