Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Program being compiled differently in 3 major C++ compilers. Which one is right?

People also ask

Which C compiler should I use?

The compiler that we recommend is the GNU Compiler collection or GCC. This is a widely used cross-platform compiler toolsuite that has libraries and compilers for C, C++, Fortran, Java, and more. Additionally the compiler that we will use later on in the course for compiling C code to run on the PIC32 is based on GCC.

Why are there different C compilers?

@pjc50: The way the standard is written effectively subdivides C into a number of disjoint dialects based upon things like the basic type of int , and will require different compilers to interpret the same source code in very different ways.

How many types of C compilers are there?

There are over 50 compilers for C like ICC by Intel to GNU GCC by GNU Project.

Why do we have different compilers?

There are several reasons (and combinations of them): Perhaps because the existing ones are not available. Compilers used to be proprietary software (and pretty pricy one). People who liked to create without paying horrendous sums of money simply sat down and wrote a new, opensource one.


GCC is correct, at least according to C++11 lookup rules. 3.4.3.1 [class.qual]/2 specifies that, if the nested name specifier is the same as the class name, it refers to the constructor not the injected class name. It gives examples:

B::A ba;           // object of type A
A::A a;            // error, A::A is not a type name
struct A::A a2;    // object of type A

It looks like MSVC misinterprets it as function-style cast expression creating a temporary C with y as a constructor parameter; and Clang misinterprets it as a declaration of a variable called y of type C.


G++ is correct as it gives an error. Because the constructor could not be called directly in such a format without new operator. And although your code calls C::C, it looks like an constructor call. However, according to the C++11 standard 3.4.3.1, this is not a legal function call, or a type name (see Mike Seymour's answer).

Clang is wrong since it even does not call the correct function.

MSVC is something reasonable, but still it does not follow the standard.