Page 1 of 1

Standard conformant C++

Posted: Tue Oct 07, 2008 2:59 am
by Bekas
What compiler do you think is more standard conformant, GCC or VC++ ?

Wait! Don't answer that. Not before considering this:
C++ 6.4p1 (selection-statements):
selection-statement:
if ( condition ) statement
if ( condition ) statement else statement
switch ( condition ) statement

C++ 6.4p2:
The rules for conditions apply both to selection-statements and to the for and while statements

C++ 6.4p3:
A name introduced by a declaration in a condition is in scope from its point of declaration until the end of the substatements controlled by the condition. If the name is re-declared in the outermost block of a substatement controlled by the condition, the declaration that re-declares the name is ill-formed.
Example:

Code: Select all

if (int x = f()) {
  int x; // ill-formed, redeclaration of x
}
else {
  int x; // ill-formed, redeclaration of x
}
if/switch/while/for statements, that's pretty common stuff, no weird templated code involved. Both compilers should be able to handle them as stated, right ?
Let's see...

VC++

Code: Select all

int f();
void g() {
  if (int x=f()) {
    int x; // error, PASS
  }
  switch (int x=f()) {
    default:
      int x; // no error, FAIL
  }
  while (int x=f()) {
    int x; // error, PASS
  }
  for (;int x=f();) {
    int x; // error, PASS
  }
}
3 PASS, 1 FAIL

GCC

Code: Select all

int f();
void g() {
  if (int x=f()) {
    int x; // no error, FAIL
  }
  switch (int x=f()) {
    default:
      int x; // no error, FAIL
  }
  while (int x=f()) {
    int x; // error, PASS
  }
  for (;int x=f();) {
    int x; // error, PASS
  }
}
2 PASS, 2 FAIL

And there's more...
C++ 6.5.3p1 (The for statement)
The for statement:
for ( for-init-statement condition[optinal] ; expression[optional] ) statement

Names declared in the for-init-statement are in the same declarative-region as those declared in the condition.
Let's do some tests..

VC++

Code: Select all

int f();
void g() {
  for (int x; int x=f();) {} // redeclaration error, PASS
}
GCC

Code: Select all

int f();
void g() {
  for (int x; int x=f();) {} // no error, FAIL
}
Ok, what are the results?

VC++: 4 PASS, 1 FAIL
GCC: 2 PASS, 3 FAIL

And the round goes to.. VC++ :)

Posted: Tue Oct 07, 2008 3:07 am
by nullsquared
My personal favorite is that VC++ allows references to temporaries, which is illegal.

Both compilers have their quirks, and if you work out all of these quirks (not just a select few), VC++ is the less C++ standard compliant one.

Posted: Tue Oct 07, 2008 5:31 pm
by PolyVox
I've heard particularly good things about the Digital Mars C++ compiler. I believe it's the only one which supports the (standard!) 'export' keyword.