Thursday, February 23, 2017

Why has the industry switched from C to C++?



First of all i would like to have a real answer, i'm always trying to get more from various sources and articles, and when I read things like C++ is slow because it has virtual functions and because of this C is better, i really don't know what to say and think as an human being with a brain. So please avoid to reach this level in your answer/s.


My question is about a massive switch to C++ that was completed, more or less, with Doom 3.


The interesting thing for me is that before this milestone, most of the game engines and the games itself were written in C, just like it was since the Quake era. It's also interesting to note that the ID software decide to completely rewrite the codebase for the IdTech 4 in C++, a massive amount of work that honestly i can't understand without a really good list of reasons.


I'm focusing on Doom 3 because I am mainly interested in the OpenGL world and in my journey i try to stay focused on this topic, so i read a lot about this, but i think that a question like that can be render-API-agnostic without too much problems.


Why at a certain point in time the industry switched massively to C++ ? What are the reasons for the choice that ID made ?



The last thing that i would like to say is that the C language is much more simple to implement and provides a less number of features, because of this has much less chance to be "fragmented" in pieces unlike the C++ really often does. In simplest terms i have much more chances to find a really good C compiler rather than a good C++ compiler with all the features implemented in a good way.


For example the NDK for Android still doesn't have a good C++ support ( with the r8b release ) with all the latest and greatest features, and it's the native toolkit for the most popular mobile OS in the world!


If I had wrote my code in a modern C++ I would probably be in pain now because one of the most popular OS in the world would be off-limits for me. And like Android, many other compilers are not that great.


I should write C++ code referring to a C++ version that is 2-3 release old ?



Answer



C++ does everything C does. You can trivially mix C and C++ in cases where the advantages of C outweigh those of C++. This is a very intentional design decision of C++.


C++ does things that C does not. This includes easy polymorphism, but also easy compile time code generation via templates. This is really handy for things like containers, which are easily C's biggest weakness. It also allows user-defined pointer-like types (smart handles) that save lots of time, as well as user-defined primitive-like types such as vectors and matrices with operator support (also saves lots of time).


Virtual functions are slower than non-virtual functions. However, you must opt in to virtual functions, and a competent programmer does so only when they're beneficial. C programmers have function pointers and often store collections of those in struct referenced by other structs; in other words, they go through extra work to recreate the exact same thing as virtual function tables. In cases where only a single function pointer is needed and no table is required, C++ still fully allows that and is equivalent to C. With a modern compiler, C++ is only slower than C in the specific cases the programmer is opting in to a feature. Also, the virtual function overhead in practice is very small on modem CPUs. Hardware today is designed for C++'s usage patterns, and is even increasingly designed for high-level interpreted langiages' needs.


C++ exceptions historically impose a lot of overhead, making C++ slower even if you're not using them. Exceptions were a terrible thing to add to C++, if for no other reason than the immense increase on complexity involved in writing exception-safe code, and in fact some container designs are literally impossible to make exception safe. Game programmers often ignore exceptions' existence, and even disable them on the compiler. Modern compilers have zero- overhead exceptions (that is, you only pay th cost for them when you actually use them).


C is simpler to learn all the rules of. C++ is a very big, complex language. C++ allows writing higher-level code, producing easier and simpler APIs. Some people want to understand the language easier, some people want to write advanced code easier. It's a trade off between simplicity of understanding what the compiler is doing with a specific piece of code vs the simplicity of writing large complex inter-connected applications. Some folks value one far more than the other, for various reasons.



In the end, C++ is a superset of C. In my opinion, there is no such this as a highly competent C++ programmer who is not also a passable C programmer (though there are a lot of C++ programmers who fall below my bar who would be lost in pure C). While C++ adds facilities to insulate the programmer from much of C, non- trivial C++ code often does need to use C to get things done. This is one of the primary difference between C++ and Java and C#. There's a reason that you often see "C/C++" lumped together, after all.


My personal belief -- which is shared with most other games industry professionals I've interacted with -- is that the enhanced expressiveness and high-level programming facilities of C++ outweigh the increased complexity of the language over C, and most of the other frequent anti-C++ claims made are simply out of date with today's technology.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...