C++ as front end to ANSI C

Jan 15, 2011 at 12:39pm
This is a bit on/off-topic.

Cfront was Bjarne Stroustrup's prototype compiler that was implemented as front end to (originally non-standardized) C. In version 2.0 it implemented multiple and virtual inheritance. (*) The language already had polymorphism. This was released in 1989. The ANSI C standard was also completed in 1989. Could Cfront's output have been ANSI C compliant? Is it possible to write a C++ compiler (even without exceptions) as front end to ANSI C? (Without sacrificing memory efficiency.)

There are all sorts of articles on the net for using object-based programming with C. But their code is either not ANSI-portable or uses redundant parent pointers in the base structure. There are no facilities in ANSI C for downcasting and no guarantees on structure alignment. Upcasting can be simulated with aggregation, downcasting can be simulated with unions by exploiting the common initial sequence rule (6.5.2.3/5 **), but only in the case of single inheritance. I think that the Cfront compiler could not have generated ANSI C code with simultaneous multiple, not to mention virtual inheritance and polymorphism support. (I'm not claiming that it should have, given the concurrency of the events then.)

* http://www2.research.att.com/~bs/hopl-almost-final.pdf
** http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n843.htm

Thanks

EDIT: It appears that there is an offsetof macro in the ANSI C standard (that is also inherited in C++) that allows you to simulate downcasting with aggregation.
Last edited on Jan 15, 2011 at 2:25pm
Topic archived. No new replies allowed.