0

Is there a way to implement this?

void func2(...) {
    /*
     * Handle „...” parameters
     */
}
void func1(int n, ...) {
    func2(...);
}
bsz
  • 61
  • 8

2 Answers2

2

No, you can't. Variadic arguments cannot be forwarded. Your choices are:

  • have your "inner" function take a(n initialized) va_list argument instead of ... and pass that list from the caller;

  • if the arguments are of the same (or convertible) types, you can make it accept an array, then parse the variadic arguments yourself and pass the array and its length to the called function.

2

This is impossible according to Wikipedia. http://en.wikipedia.org/wiki/Stdarg.h

Variadic functions are functions which may take a variable number of arguments and are declared with an ellipsis in place of the last parameter. An example of such a function is printf. Variadic functions must have at least one named parameter, so, for instance, char *wrong(...); is not allowed in C. (In C++, such a declaration is permitted, but not very useful.) In C, a comma must precede the ellipsis; in C++, it is optional.

So your void func2(...) is illegal.

turnt
  • 3,235
  • 5
  • 23
  • 39
  • 1
    This answer is correct, but not useful, since it doesn't address the actual question (which is about forwarding varargs), and since the OP's example is easily fixed to address the problem you describe. (I'm neither the upvoter nor the downvoter, BTW, though I do understand where both voters were coming from.) – ruakh Nov 29 '13 at 19:38
  • What's written here is is true, but this wasn't the question. –  Nov 29 '13 at 19:38
  • Yeah, I think I interpreted the question differently. I was stating that is is illegal to write something like ```void func2(...)``` – turnt Nov 29 '13 at 19:40
  • @NoWiS Yes, but nevertheless this still does not answer OP's question. This is an answer to "can I declare a variadic function without a leading named parameter?", which is **not** what OP is looking for. Yes, OP posted incorrect code, and only he is to be blamed for that only, but a non-answer is a non-answer. –  Nov 29 '13 at 19:42
  • @H2CO3 I see your point. I'm sorry. – turnt Nov 29 '13 at 19:51