28

Possible Duplicate:
“C subset of C++” -> Where not ? examples ?

I'm aware that C++ is not a strict superset of C. What language features prevent C++ from being a superset of C?

Community
  • 1
  • 1
Mike
  • 23,892
  • 18
  • 70
  • 90
  • 1
    This question has been answered a 1000 times, I'd like to see a technical answer that demonstrates it's simply not possible, keywords and C99 aside... – Matt Joiner Sep 23 '10 at 09:41
  • 1
    @Matt Joiner: why put C99 aside? – JeremyP Sep 23 '10 at 09:43
  • 1
    @Matt: demonstrate that *what* isn’t possible? Of course every C program can be refactored to valid C++. That is rather trivial, given that both languages are Turing complete. – Konrad Rudolph Sep 23 '10 at 09:43
  • 1
    @Konrad: It's nearly that trivial, but not quite. C is Turing complete and also provides access to a filesystem. Its computing model has outputs that are not the same as the outputs of Turing's model. A hypothetical language which was Turing complete but did not provide access to a filesystem, would not be a superset of C in the "can be refactored" sense. But because C++ provides access to all C's libraries and to `volatile` memory, we're looking good on the I/O score. – Steve Jessop Sep 23 '10 at 10:36

5 Answers5

57

The elephant in the room: the following is valid C but not valid C++.

int typename = 1;

Substitute your favorite C++ reserved word.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
24

C++ also does not support variable-length arrays, where:

int array[n];

is valid in C, but not C++. A C++ version of the above would be:

int *array = new int[n];
  ...
delete [] array;
Alexander Rafferty
  • 6,134
  • 4
  • 33
  • 55
12

There is a special wiki entry that summarizes a lot of issues.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
9

Simple example, consider this declaration:

int f();

This is valid C, but invalid C++: f(3, 2, -5, "wtf");

Explanation: in C, int f() is treated like int f(...) (at least at the first call site). Declare as int f(void) if you don't want f to take parameters.

Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
  • 5
    No, `int f()` is not really `int f(...)` , there's a big semantic difference. In the first case, this means I don't know the parameters of that function and at the first encounter of the function call, the default types used as parameters declares the signature of that function, subsequent calls must adhere to that implicit prototype and the compiler should warn if you call the function with other params. With the ellipse it's not the case, each call can have different parameters without warning. – Patrick Schlüter Sep 23 '10 at 13:02
  • 1
    @tristopia: Whatever it is, it's really a *don't do this* thing. – Alexandre C. Sep 23 '10 at 13:11
  • Absolutely, I was just nitpicking as there is a real but subtle difference. This said I haven't downvoted your contribution as the message is mainly ok. – Patrick Schlüter Sep 23 '10 at 14:51
3

One from top of my head - C++ does not support default int.

Karel Petranek
  • 15,005
  • 4
  • 44
  • 68