Is there any way to specify a default parameter in a variadic function?(Applies to templates also)
-
What actual problem are you trying to solve? What language? – GManNickG Nov 09 '10 at 04:53
-
Your whole question clearly addresses C++ and not C. There are no default arguments for functions in C (with the `=something` notation as you seem to assume that) and not templates either (as Kirill alrready noted). – Jens Gustedt Nov 09 '10 at 10:43
4 Answers
In C++ you can replace the variadic function with one based on the Named Parameter Idiom.
See the C++ FAQ item 10.20 What is the "Named Parameter Idiom"?.
That gives you default functionality & convenient notation.
Cheers & hth.,

- 1,418
- 3
- 20
- 36

- 142,714
- 15
- 209
- 331
Why would you need both variadic and default params?
For example,
myFunc(int a=5, int b=5, int c=5);
can receive 0-3 parameters with default values, and
myFunc(...)
can reveive any number of parameters. Inside the function, you can check for missing parameters and fill in the default values as required.

- 6,134
- 4
- 33
- 55
First a C++ answer.
A default parameter is a parameter for which you will know that the function should and will see as provided. So you should definitively name these parameters and then may provide default arguments. This would be a "short" version of your function.
If in addition to these default arguments behind you want to have the possibility of having a va_arg
argument list just overload your function with a second version that does exactly this. For that "long" version you have to provide all arguments anyhow, so there would be no sense in having default arguments, here.
Now a C answer
Probably you were not looking into such a thing, but with the va_arg
macro features of C99 it is possible to define default arguments for functions in C, too. The macro syntax then is more permissive than it is for C++ in that you may also omit arguments in the middle of a function call, not only at the end. So if you would have declared your function something like
int toto(int a, ...)
and specified default arguments for positions 2 and 3, say, you could call it as
toto(4,5,,,37);
So in this sense in C it is possible to do what you asked for. I personally would certainly hesitate to do this.

- 76,821
- 6
- 102
- 177
-
-
@Alexander: they may be a corner case of the language, but still they exist for whatever reasons. – Jens Gustedt Nov 09 '10 at 21:16
-
Hmm.. C++ does have variadic parameters. I thought this was a C-only thing, like VLAs. – Alexander Rafferty Nov 10 '10 at 06:41
-
-
you can use va_arg in c++, its not that great though, the only way to use it safely is to wrap a macro around it which terminates the thing with a zero parameter, which there is no way of doing this normally, printf, for example, just looks at how many formatters and assumes that many parameters, i think there are some compiler optimizations for that but like i said, not very safe. Check out the __VA_ARGS__ macro, you can make it (safer, not safe since others could still call around your macro and there is no access control when it comes to macros). I say avoid va_args like the plague.. :) – osirisgothra Nov 17 '13 at 11:00