William Pursell
William Pursell

Reputation: 212534

Why is the comma optional in C++ variadic function declarations?

Is there a difference in these two declarations?

int foo( int a, ... );

and

int foo( int a ... );

If there is no difference, what was the point of making the second syntactically valid?

Upvotes: 8

Views: 491

Answers (1)

CB Bailey
CB Bailey

Reputation: 792847

This is speculation, but in C++ in can make sense to have a function with no other parameters, e.g. void f(...) whereas in C a function like this has no use (that I know of) so ... must follow some other parameter and hence, a comma.

From a grammar point of view, it's simpler to simply allow void f( int a ... ) and give it the obvious meaning than it is to disallow it and it's not going to cause much of a burden on compiler writers or any confusion for programmers.

(I originally thought it might be something to do with making the grammar for parameter packs more regular but I discovered that it was explicitly allowed in C++03 in any case.)

Upvotes: 2

Related Questions