C++ philosophy and design

C++ started as a humble preprocessor for C, so it's only natural that it inherited C's low-level character with its direct map to hardware, memory allocation control (stack and heap), pointer arithmetic, casts between types, dangerous lack of boundary checks, and also that of implicit initialization. Much of the early success of C++ has its roots in the decision to maintain backward compatibility with C in order to be able firstly to compile old C sources, and secondly to maintain the high performance of the resulting code.

Hence, one of the first and most important design guidelines for C++ was, and still is, the zero-overhead abstractions principle, or rephrased, you don't pay for what you don't use. The meaning of it is that each new language feature should incur no overhead if it is not used. For example, if we don't explicitly use virtual functions there's nothing added to the class definition, whereas in Java every method is virtual, and we have to mark it as final if we don't want to pay the price. Another example could be garbage collection support, which is optional in C++.

Other cornerstones of the C++ philosophy were the use of constructors and destructors for automatic resource management (the famous resource acquisition is initialization (RAII)), support of polymorphism and multiple inheritance and sometimes unbridled usage of template wizardry and operator overloading. You might feel that this is a lot of additional features crammed in on the old C language's base and might ask yourself whether C++ still pursues the high-performance goal. So, let me cite from a recent document of the standard committee titled, Directions for C++ (available on https://wg21.link/P0939):

Technically, C++ rests on two pillars: A direct map to hardware (initially from C) and zero-overhead abstraction in production code (initially from Simula, where it wasn't zero overhead). Depart from those and the language is no longer C++.

This sums up the C++ philosophy nicely—while maintaining a link to low-level functionality as low-level languages do, it offers advanced high-level abstractions like the most sophisticated languages out there. And that's with zero overhead! That's why we love C++—not many languages are able to pull off this trick!

From the point of view of our performance assessment of C++, the most important principle is the zero-overhead principle. By and large, this principle was adhered to when designing the classic C++ features with two notable exceptions—exceptions and run-time type identification (RTTI). Another basic feature that stirred up much controversy in its time was the cost of the virtual functions. We, hence, will shortly discuss these two features and their corresponding performance impacts.