Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weird compilation output

Tags:

c++

c

gcc

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

like image 751
FrozenHeart Avatar asked Aug 26 '12 18:08

FrozenHeart


1 Answers

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.

like image 153
Jesse Good Avatar answered Nov 15 '22 17:11

Jesse Good