tags:

views:

368

answers:

6

Does C++ code gets converted to C before compilation ?

+6  A: 

Not in most modern compilers.

The original C++ compiler was actually a preprocessor however. It generated C code, whaic was then compiled by a C compiler.

John Dibling
Comeau still does it.
David Rodríguez - dribeas
+5  A: 

In the early days of C++ compilers, some did it that way. I haven't seen a C++ compiler implemented that way since the late 1980s however.

wallyk
Comeau C++ is still implemented this way.
Chris Lutz
+12  A: 

No, but like most myths there's a shred of truth to this. The original compiler for C with classes (which later became C++) was nicknamed CFront and did translate to C.

dsimcha
+19  A: 

A few C++ comilers (the original cfront, Comeau C++) use C as an intermediate language during compilation. Most C++ compilers use other intermediate langauges (e.g. llvm).

Edit: Since there seems to be some misunderstanding about the history: "C with classes" started out using a preprocessor called "Cpre". At that time, it was seen strictly as a dialect of C, not a separate language in itself. In December 1983, people were starting to view it as a separate language, and the name C++ was invented. As it happens, development of cfront started in April 1983, so a reasonably usable version became available (to a select few) just about the same time as the name "C++" came into use. This appears to be mostly coincidence though, not a plan.

As far as producing C as its output, that was really quite common on Unix. Just for example, the Berkeley Pascal compiler and at least a couple of Fortran compilers also produced C as their output.

There is, however, a huge difference between Cpre and Cfront. Although both produced C as their output, Cpre did virtually no syntax checking of its own -- it looked for a few specific things, and did a relatively mechanical translation on them. It wasn't until the C compiler looked at the result that real syntactical analysis was done. If your code contained a syntax error, it was almost certain that it wouldn't be caught until the C compiler parsed the output from Cpre.

Cfront, however, did full syntactical analysis of the source code itself, so (short of a bug in its code generator) you'd never see a syntax error from the C compiler. The C compiler was simply used as a code generator so nobody needed to rewrite CFront to accommodate different processors, object file formats, etc.

If you want to get into more detail, chapter 2 of The Design and Evolution of C++ is devoted almost entirely to the "C with Classes" time frame (and there are various other details about it spread throughout the book).

Jerry Coffin
+2  A: 

As others have answered. NO.

However if you want to use an OOP language like C#, and have your code compiled into C I recommend you take a look at Vala.

Mihai Lazar
This is largely irrelevant to the actual question being asked, and looks a lot like spam if it didn't come from a user with 604 reputation. I suspect you're just trying to be helpful however, so I won't penalize you, but this answer isn't terribly helpful to a C++ user.
Chris Lutz
+2  A: 

the title seems to ask is C++ a superset of C, i.e. can you just dump any c code in a c++ compiler and it will work? In which case, yes it is, sort of...

one major difference is that C automatically casts pointers for you, c++ does not, you need to cast the manually...

any one remember anything else?

thats all I remember from the horrible process of converting a massive C project to compile under c++ for some reason...

matt
Most C99 features (the ones not borrowed from C++) are not in the current standard, some will not be in the upcoming standard either. Most notably dynamic arrays. In C++ the size is part of the type of the array, and allowing dynamic arrays would imply that there is an element in the language whose type would not be known at compile time, breaking the type system.
David Rodríguez - dribeas
That's too strong a statement actually; you can define the type systems such that "dynamic array of type T" is the compile-time type. That would imply that the array size is only part of the type when an array is not dynamic, and therefore static and dynamic arrays would have different types. Not a big deal IMO; in fact it sounds obvious. The trickier parts are wrt to `sizeof` - it's not always an ICE anymore.
MSalters