Functions with different value of default arguments

100 views Asked by At

How to parse function arguments, when all have default values and only some of them are used? Example: I have a function with several arguments with default values

void fun(string a="", int b=0, string c="", int d=0)
{
 //parse used arguments somehow
}

I want to use it with different value of arguments, for example:

fun("foo", 10);
fun(10, 10);

How can I determine which arguments were used? Maximum value of arguments is known and order will be always the same. I do not want to run function like:

fun("", 3, "", 10);

And I cannot use variadic functions.

Any ideas?

1

There are 1 answers

0
timrau On BEST ANSWER

Overload the function.

void fun(int b, int d) { fun("", b, "", d); }

Then fun(3, 10) becomes equivalent to fun("", 3, "", 10).