I’ve just compiled this code:
void foo(int bar...) {}
int main()
{
foo(0, 1);
return 0;
}
And the compilation output was really weird:
g++ test.c
Output:
Nothing
and
gcc test.c
Output:
test.c:1:17: error: expected ‘;’, ‘,’ or ‘)’ before ‘…’ token
I know that there is no comma after parameter, this question about strange compilation output.
I understand why this is invalid in C, but cannot understand why it is valid in C++.
The other answer is correct (I upvoted), but just to give a reference [8.3.5 Functions clause 3]:
This means that the comma is optional in C++, but not in C. You can also write
void foo(...)in C++, because the parameter declaration list is also optional.As for the reason why, in C++ templates,
test(...)is common when using SFINAE for a "catch-all" function. However, in C, there is no usage forfoo(...)and hence it is illegal.