Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

syntax

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?

like image 876
William Pursell Avatar asked Nov 25 '11 21:11

William Pursell


People also ask

What is a variadic function in C?

Variadic functions are functions that can take a variable number of arguments. In C programming, a variadic function adds flexibility to the program. It takes one fixed argument and then any number of arguments can be passed.

What is Va_list in C?

va_list is a complete object type suitable for holding the information needed by the macros va_start, va_copy, va_arg, and va_end. If a va_list instance is created, passed to another function, and used via va_arg in that function, then any subsequent use in the calling function should be preceded by a call to va_end.

What is args C++?

Argument. Parameter. When a function is called, the values that are passed during the call are called as arguments. The values which are defined at the time of the function prototype or definition of the function are called as parameters.

What is ellipsis in C++?

Ellipsis in C++ allows the function to accept an indeterminate number of arguments. It is also known as the variable argument list. Ellipsis tells the compiler to not check the type and number of parameters the function should accept which allows the user to pass the variable argument list.


1 Answers

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

like image 113
CB Bailey Avatar answered Oct 18 '22 20:10

CB Bailey