Default arguments and variadic functions
Asked Answered
M

4

11

Is there any way to specify a default parameter in a variadic function?(Applies to templates also)

Mustache answered 9/11, 2010 at 4:50 Comment(2)
What actual problem are you trying to solve? What language?Dulcedulcea
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).Lifelike
C
9

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.,

Capricorn answered 9/11, 2010 at 5:1 Comment(0)
A
3

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.

Afteryears answered 9/11, 2010 at 5:2 Comment(0)
L
3

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.

Lifelike answered 9/11, 2010 at 11:0 Comment(5)
C++ doesn't support variadic funtions by my understanding.Afteryears
@Alexander: they may be a corner case of the language, but still they exist for whatever reasons.Lifelike
Hmm.. C++ does have variadic parameters. I thought this was a C-only thing, like VLAs.Afteryears
Can the va_arg macro feature be used in C++ ?Sliwa
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.. :)Clarkin
M
2

No there is not way of doing that.

Melpomene answered 9/11, 2010 at 4:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.