Extra semicolons in C++.

Jason Cipriani jason.cipriani@gmail.com
Thu Jan 24 16:22:00 GMT 2008


Hello all,

I have been wondering this for a really long time now, since it was
changed in GCC. In a C source file compiled with -pedantic, an extra
semicolon would produce the following error, of course:

test.c:9: warning: ISO C does not allow extra `;' outside of a function

I'm assuming this is for the reason it says: because ISO C doesn't
allow extra semicolons.

What I've always wondered is, why does compiling a C++ file with g++
-pedantic produce errors about extra semicolons as well?

test.cpp:9: error: extra `;'

I though that extra semicolons were perfectly valid in C++. Is this
not true? Somebody on another mailing list recently pointed out to me
(and this is what brought up this question), that the C++ standard
says:

   statement:
expression-statement
compound-statement
...

   expression-statement:
expression_opt ;

   compound-statement:
{ statement-seq_opt }

And the _opt items are optional. So a single semicolon and nothing
else is a valid "expression statement" and therefore a valid
"statement". Is something being interpreted wrong here?

Thanks!
Jason

P.S. Here is the test program:

int main (int argc, char **argv) {

  if (0) {
  } else {
  };

  return 0;;

};



More information about the Gcc-help mailing list