Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comma omitted in variadic function declaration in C++

I am used to declaring variadic functions like this:

int f(int n, ...); 

When reading The C++ Programming Language I found that the declarations in the book omit the comma:

int f(int n...); // the comma has been omitted 

It seems like this syntax is C++ specific as I get this error when I try to compile it using a C compiler:

test.c:1:12: error: expected ‘;’, ‘,’ or ‘)’ before ‘...’ token int f(int n...);

Is there any difference between writing int f(int n, ...) and int f(int n...)?

Why was this syntax added C++?

like image 451
wefwefa3 Avatar asked Feb 29 '16 19:02

wefwefa3


1 Answers

According to § 8.3.5.4 of the C++ standard (current draft):

Where syntactically correct and where “...” is not part of an abstract-declarator, “, ...” is synonymous with “...”.

In short, in C++ ... (ellipsis) is an operator in its own right and so can be used without the comma, but use of the comma is retained for backwards compatibility.

like image 133
kfsone Avatar answered Sep 23 '22 08:09

kfsone