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]:
parameter-declaration-clause:
parameter-declaration-listopt...opt
parameter-declaration-list , ...
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 for foo(...)
and hence it is illegal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With