Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ implemented in plain C [duplicate]

I have read several times that early C++ compilers translated first C++ code into plain C before compiling it (or maybe needed a third-party C compiler).

Playing myself with grammar / language / compilation fields, I am curious to see how C++ was implemented in plain C, especially what can be one way to implement the class inheritance and [virtual] method calling.

Could you point me to such a compiler that would still be available nowadays?

I know that OO code can be simulated / emulated in plain C with structs and functions pointers, but I would like to see an actual implementation of the C++ language in C.

like image 459
Seki Avatar asked Apr 12 '13 11:04

Seki


4 Answers

You can try cfront. You can download old versions here. But it only supports a very limited C++ subset. Some features like exceptions can not be implemented this way.

Update: As Maxim Yegorushkin noted Exceptions might be implemented with setjmp/longjmp. But if I remember correctly exceptions can not be implemented as an library in C++. They have to be part of the core language.

like image 160
Jan Herrmann Avatar answered Nov 12 '22 18:11

Jan Herrmann


If you just want to see how C++ can be translated in C then you have several options available to you and the C++ FAQ has a section that covers this here. It cover all the major options I have ever seen suggested and should be updated when new options are available.

like image 33
Shafik Yaghmour Avatar answered Nov 12 '22 17:11

Shafik Yaghmour


Comeau compiler works this way. In its heyday everyone was praising its standard compliance and used Comeau online to test snippets of code, but few were using it for building production codes.

EDGE frontend also works this way. I hear it is used by both Intel C++ compiler and Comeau.

like image 28
Maxim Egorushkin Avatar answered Nov 12 '22 16:11

Maxim Egorushkin


The first Microsoft C++ compiler was exactly as you describe, I know because I remember using it. From my memory, I think it was version 7 of their C compiler. This would have been around 1992 (plus/minus 2 years).

Update: see http://en.wikipedia.org/wiki/Visual_C%2B%2B, the one that I'm referring to was indeed released in 1992 and called "C/C++ 7.0"

like image 44
Stochastically Avatar answered Nov 12 '22 16:11

Stochastically