views:

40

answers:

3

Visual studio 2008 isn't catching syntax errors. for example: Tpc_feed(void); compiles fine. this: Tpc_feed(void);;;;;;; compiles fine, but this Tpc_feed(void) catches an error. Are the extra semi-colons not syntax errors? I'm on Windows 7 32 bit and VS C++ 2008

+7  A: 

Technically, it's a syntax error, but most compilers allow it anyway. GCC even allows it by default unless you compile with -pedantic.

In any case, it's against the standard so you should get rid of the extras.

zildjohn01
I know that I should get rid of them, but why do compilers allow it?
TheFuzz
Who knows? Compilers aren't perfect. It's probably not a high priority to fix because it (1) has no effect on correct programs, and (2) it's easy to guess what the coder's intent was when they type like that.
zildjohn01
+1  A: 

Without a return type, none of them should compile as C++. Assuming a return type is added, the extra semicolons are allowed on member function declarations, but if the declarations are outside a class/struct they're prohibited.

I don't believe VC++ has a flag to force them to be flagged as errors.

Edit: Actually (going back and re-checking things), an unnecessary semi-colon is allowed following a member function definition, but I think you're right, and it's not allowed following a member function declaration.

Jerry Coffin
its my class constructor, so doesn't it compile as C++?
TheFuzz
Oh, okay -- yes a ctor doesn't need to (and in fact, can't) specify a return type. My apologies.
Jerry Coffin
A: 

As others have already said, it's not a valid C++. The reason why many compilers support it anyway is that it makes creating macros a bit easier/more intuitive, consider this:

// Declares a function, and because the semicolon belongs to the declaration, the macro adds it
#define MY_FUNCTION_DECL(returnType, arguments) returnType myFunction arguments;

// Calling macros looks like functions, the semicolon looks natural
MY_FUNCTION_DECL(void, (int, int, int));  // There are two semicolons now

This example is somewhat weird as I made it up right now but it should represent the idea behind accepting more semicolons as valid.

dark_charlie