0

Would it be theoretically possible to implement C++ keywords, classes syntax etc. using a C library with right macros? (i.e. prepare a library which would make any C++ code compile using C compiler). I guess the answer is probably "no", but I wonder if you can prove it.

  • 1
    http://stackoverflow.com/questions/351733/can-you-write-object-oriented-code-in-c – Oliver Charlesworth Apr 20 '16 at 19:32
  • If you want to convert any valid C++ program into a C program then that is going to be a big challenge. I do not know if you would be able to replicate move semantics in C. – NathanOliver Apr 20 '16 at 19:34
  • Try doing this and tell us what you have come up with. – milleniumbug Apr 20 '16 at 19:35
  • I'm curious why anyone would _want_ to do that. In any case, I doubt that it could be done with macros. But I don't think that not doing it would rigorously prove that it can't be done. – Logicrat Apr 20 '16 at 19:36
  • 2
    I dont really agree that the question is a duplicate. Writing oo code in c might be doable, while transforming c++ to c is much more challenging – 463035818_is_not_an_ai Apr 20 '16 at 19:37
  • As apparently basically everything, including `mov`, is turing complete, one can probably proof that it is possible (not a CS guy though). However, in the real world, no one cares. Maybe try your luck on CS.SE if you have enough academic interest, but read their rules first. – Baum mit Augen Apr 20 '16 at 19:38
  • 2
    No. Not by simple text substitution (which is what macros are). You won't get past the first template. – StoryTeller - Unslander Monica Apr 20 '16 at 19:39
  • Actually, I'm curious why anyone would use **C** when **C++** exists? There aren't many situations in which a C program can do something a C++ program can't. – Logicrat Apr 20 '16 at 19:42
  • 4
    @Logicrat 1) Legacy code 2) Not every platform has a decent C++ compiler, esp. in the embedded world 3) If you are Torwalds, you want to keep the CS-C++ noobs from screwing up your project 4) You believe that C is faster, and probably many more. – Baum mit Augen Apr 20 '16 at 19:44
  • 1
    Perhaps this applies here in some tangential fashion. C++ compilers were _initially_ code translators, converting C++ syntax to C. and then compiling with a C compiler. So BITD, _all_ C++ programs could theoretically be translated into C code. – chux - Reinstate Monica Apr 20 '16 at 19:46
  • @chux, while what you are saying is true in general, it doesn't mean program can translate itself. – SergeyA Apr 20 '16 at 19:49
  • There are OO libraries in C that extensively use macros for implementing their OO functionality, but the final product don't look like C++ code would. One example is _GObject_. – Paulo1205 Apr 20 '16 at 19:53
  • If it were possible, we wouldn't have needed Cfront. – Raymond Chen Apr 20 '16 at 19:57
  • 4
    I'm voting to close this question as off-topic because it is about a theoretical issue (language equivalency), and not a practical programming problem. – abelenky Apr 20 '16 at 19:58
  • Keep in mind that the first C++ implementation, cfront, parsed C++ (or a subset of it) and produced C code as its output. Here is some info on [cfront](https://en.wikipedia.org/wiki/Cfront). – Tom Karzes Apr 20 '16 at 20:13
  • Let's say *it was possible* - `cfront`used to be not much more than a preprocessor. Modern C++ has probably migrated away too much from C to make this still possible. – tofro Apr 20 '16 at 20:37

4 Answers4

4

C does not have function overloading, but C++ does.

It seems to me that it would probably be impossible to make this simple C++ code compile in C:

bool Add(int a, int b);
bool Add(string a, string b);

(two overloaded functions: same name, different parameters, different implementation)

C would report an error similar to "redefinition of an existing function".
C++ would compile it with no problems.

abelenky
  • 63,815
  • 23
  • 109
  • 159
2

No.

For a specific proof, consider this template.

template<size_t n>
struct fact {
  static const int value = n * fact<n-1>::value;
};

template<>
struct fact<0> {
  static const int value = 1;
};

Even if you could write an extremely sophisticated preprocessing macro to translate this into C, the preprocessor only runs once. It does not loop or run recursively (which this template definition requires to function correctly.) So you cannot implement this template within C with macros only.

You may be able to do a subset of C++, but the preprocessor is fundamentally unsuited for this situation.

EDIT: Re: Boost.Preprocessor. Iteration is faked in the Boost.Preprocessor. There is an iteration limit of 256 in all cases, this is because the preprocessor fakes loops using repeated calls. See boost/repetition/for.hpp for an example.

OmnipotentEntity
  • 16,531
  • 6
  • 62
  • 96
2

The template mechanism is turing complete. The macro processor is not. /story

Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
0

Operator overloading might represent an issue. You can go with code generation, g++ frontend probably does something of this kind. Fork it and fix it.

bipll
  • 11,747
  • 1
  • 18
  • 32