Outputing data for variadic function

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?

If the function accepts a list, you can pass a list...

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.

Probably not, resolution of the variadic arguments is a compile time thing, not a run time thing.

I think you can take the argument list passed to your variadic function and pass it to another variadic function.

See:
https://en.cppreference.com/w/cpp/utility/variadic
https://en.cppreference.com/w/cpp/language/variadic_arguments
https://en.cppreference.com/w/c/variadic/va_start

I went through all three of them, but still cant figure out how when getting

bool dispatcher(uint_8 *atribs, ...) {
 // Now acting on atributes and decide wether to preprocess the variables

I still don't know how to pass the variadic input list through to foo which takes input in format

void foo(reference,...);

For a moment I have build a dirthy PHP code generator that givews me the long case style option

<?php
	$a=0;
	$length=20;
	$permanent="format";
	$lister="buff";	
	if($length<1) printf("\t\tprinft(%s);\n",$permanent);
	else {
		print("\tswitch(vystupov){\n");
		for($i=0;$i<$length;$i++){
			printf("\t\tcase %d:{\n\t\t\tprintf(%s",$i,$permanent);
			for($j=0;$j<$i;$j++) printf(",%s[%d]",$lister,$j);
			printf(");\n\t\t\t} break;\n");
		}
		printf("\t}");
	}
?>

this generates a very ugly code that deals with the problem the bad way.

Without the library headers of your function, it is impossible to answer your question...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.