Note: For technical reasons, the title had to be changed. The real title of this article is "C++ is not C"
I've been ranting about this on the net for quite some time now, so I think it's a good idea to kinda dump all of the random thoughts I've had on the subject here. So I don't have to constantly repeat myself.
I dabble in C/C++. I am a C/C++ programmer.
First off, the two are completely different languages. Not only are the two languages completely different, they are not even compatible.
In my eyes, this is a failure of C++. C++ claims to be a "safe", "backwards-compatible" "superset" of C. It is none of those, and it shouldn't even be named C++.
To understand why this is the case, we'll have to look at some facts:
Back in the old days, before C was officially standardized by ANSI (which wouldn't happen until 1989), there were many, many dialects of C. It was almost as bad as the assembly situation. Although to C's credit, most dialects shared a lot of the same features.
Then came C with classes (1979) and Objective C (1984). Two dialects of C that attempted to further-explore Object Oriented design philosophy. Basically, Objective-C got it right, and C with classes got it wrong. However, C with classes quickly became the more popular OO variant, rivalling K&R C.
This may have been where they really went wrong. In 1983, Mr. Stroustrup got cocky and renamed CWC to C++, as a statement saying that it's better than C and the BDFL of C++ will ensure that C++ is tightly controlled
And then, in 1989, C was standardized by ANSI. C++ wouldn't be standardized until 1998, nine years later.
There's already some huge problems here. Now we have two languages with separate standards, and yet one still insists on keeping a name that is no longer accurate. It is not a "dialect" of C. It can't be. C is standardized. A language that doesn't follow the C standard is simply not C, and will never be 100% compatible with C. To further exacerbate this, C++ has a standard of its own, which means saying C and C++ are even alike is as absurd as saying Java and JavaScript are alike.
(or better yet, a car and carpet)
So that's the first issue. At a fundamental level, the two languages are different and incompatible, and C++'s name is wrong and misleading
First off, there is no "backward". Pre-ISO C++ may have been compatible with K&R C back in the 1980's, but that changed once ANSI published the 1989 Standard. At that point, C became a true high level language, and C++ was still flying by the seat of its pants. C++ went from being a vertical successor to a horizontal competitor.
Second, C++ is not compatible with C at all. The only experiment needed to prove this is to take any suitably large C codebase, and attempt to compile it with a C++ compiler. Especially C code that takes advantage of C idioms, like those afforded by C's weak type system
int *array = malloc(sizeof *array * 100); // Perfectly valid C (weak typing). Throws an error in C++ (strong typing/pointer conversion).
// --
sum(a, b){ // Perfectly valid C (K&R declaration compatibility). Throws errors in C++ (syntax error).
return a + b;
}
// --
int main(void){
return; // Valid but discouraged in C (weak typing/UB). Throws an error in C++ (int() must return int)
}
// --
struct foo {
int data;
};
// ...
foo f = {5}; // valid in C++ (automatic struct tag aliasing). Throws an error in C (What's a "foo"?)
Fun fact: "//" style comments are discouraged in C, and didn't become part of the standard until C99. C++ supported single line comments far longer. Yet another incompatibility.
As you can see, there is a wealth of code that works in one language but not in the other. The fact that this happens means that existing codebases will fail to compile. I think I've made a pretty strong case against C++ and C being compatible at this point. They're clearly not.
C++ is not a superset of C. At best, it is a language that looks like C and has small bits of overlap. But this is also true of, say, python3 and perl, where print("Hello World")
is a valid hello world program. There's overlap, sure, but obviously python and perl are nowhere near alike. In fact, syntax-wise, perl is far more similar to C and JavaScript. So stop saying C and C++ are alike.
Just because polyglot code can be written that works between two different languages, does not make the two languages similar. Actually, it means you wrote a polyglot. Polyglots, like codegolfs and obfuscation contests, are cool for programming challenges or as a novelty, but not for code that is meant to be useful. If you write polyglot code, you are cutting yourself off from the advantages of both languages.
So, for example, if you ever find yourself writing int *array = (int *)malloc(sizeof *array * 100);
, then you are in the wrong no matter what. In C, that is unnecessary polyglot nonsense, and you are disadvantaging yourself by repeating yourself (repeating yourself), In C++, while it is valid, it is not a C++ idiom and you should be using the new
keyword meant specifically for allocating memory (plus, RAII is a thing). Speaking of idioms, that leads me to the next fact.
C aims to be a compact, powerful, portable, you're-on-your-own language. C++ aims to be, well, what C# is. Where C++ fucked up in that mission, was its insistence on clinging to the C name, and thus its insistence on trying to be compatible with C, despite it becoming more and more infeasible the further apart the two languages drift.
In order to be a successful language, C++ needs to let go of C. It needs to let go of Undefined Behavior, and it needs to let go of any notion that they are somehow "compatible". Of course, it can't let go of UB without sacrificing some speed, but if your goal is to be safe, then you really can't have both.
C doesn't claim to be safe. C knows exactly what it is, and it doesn't try to reassure you on anything. If you fucked up, you fucked up, and demons will fly out of your nose regardless. C++ tries to tell you that the demons are fairies. As if that's somehow better. Stroustrup's joke about how C++ blows your whole leg off is true in probably ways he didn't intend. He intended to say that C++ is powerful, but really, C++ is such a clusterfuck of good and bad (but mostly bad) ideas that there is no way to really write good, clean, efficient, bug-free code without sticking to a small, limited portion of the language. And more often than not, that portion tends to so similar to C... that you might as well just be writing C.
Similar to Java, it's too easy to write total and utter crap with C++ (although C++ is arguably worse). And most devs tend to do exactly that. Really, C++ and Java are quite good when wielded with the right hands. But C++ almost always gets used by CS students who haven't developed good taste, and Java is almost always used by dime-a-dozen corporate devs who write buggy, clunky, slow, incomplete, half-implemented, absolute garbage with it. It's not (always) the language's fault. But in the case of C++'s dubious goals, it is.
Not even the idioms are the same. Being a great C programmer does not make you a good C++ programmer (and vice versa). In fact, more often than not, it makes you a terrible developer in the other languages, because their idioms are often contradictory. That means the basic conventions on how things are done, are done completely different in both languages. If you're starting a C project, do not hire C++ devs. Likewise, if you're starting a C++ project, be weary of C devs.
I don't even need an argument for this. I just need two words: Undefined Behavior.
There's a whole quora post about why people dislike C++ here
I've even added my own answer to it, which might be one of the biggest rants I've published about it. Here it is.
Permanent Link to this page: http://bradenbest.com/tutorials/get_page.php?path=tuts%2Fc%2F&name=CPP+is+not+C
Have an idea for a tutorial? Go to the Suggestion Box