Criticism of C++


is a general-purpose programming language with imperative, object-oriented, and generic programming features. Many criticisms have been leveled at C++'s design by well-known software developers including Linus Torvalds, Richard Stallman, Joshua Bloch, Rob Pike, Ken Thompson, and Donald Knuth.
C++ is a multi-paradigm programming language with extensive, but not complete, backward compatibility with C. This article focuses not on C features like pointer arithmetic, operator precedence or preprocessor macros, but on pure C++ features that are often criticized.

Slow compile times

The natural interface between source files in C/C++ are header files. Each time a header file is modified, all source files that include the header file should recompile their code. Header files are slow because they are textual and context-dependent as a consequence of the preprocessor. C only has limited amounts of information in header files, the most important being struct declarations and function prototypes. C++ stores its classes in header files and they not only expose their public variables and public functions but also their private functions. This forces unnecessary recompiles of all source files that include the header file, each time when changing these private functions. This problem is magnified where the classes are written as templates, forcing all of their code into the slow header files, which is the case with the whole C++ standard library. Large C++ projects can therefore be relatively slow to compile. The problem is largely solved by precompiled headers in modern compilers.
One suggested solution is to use a module system. A module library is planned to be released in C++20, with future C++ releases planning to expose the functionality of the standard library using modules.

Global format state of

C++ unlike C relies on a global format state. This fits very poorly together with exceptions, when a function must interrupt the control flow, after an error, but before resetting the global format state. One fix for this is to use Resource Acquisition Is Initialization which is implemented in Boost but is not a part of the C++ Standard Library.
The global state of uses static constructors which causes overhead. Another source of bad performance is the use of std::endl instead of '\n' when doing output, because of it calling flush as a side effect. C++ is by default synchronized with which can cause performance problems. Shutting it off can improve performance but forces giving up thread safety.
Here follows an example where an exception interrupts the function before std::cout can be restored from hexadecimal to decimal. The error number in the catch statement will be written out in hexadecimal which probably isn't what one wants:

  1. include
  2. include
int main
It is acknowledged even by some members of the C++ standards body that the iostreams interface is an aging interface that needs to be replaced eventually. This design forces the library implementers to adopt solutions that impact performance greatly.

Iterators

The philosophy of the Standard Template Library embedded in the C++ Standard Library is to use generic algorithms in the form of templates using iterators. Early compilers optimized small objects such as iterators poorly, which Alexander Stepanov characterized as the "abstraction penalty", although modern compilers optimize away such small abstractions well. The interface using pairs of iterators to denote ranges of elements has also been criticized. The C++20 standard library's introduction of ranges should solve this problem.
One big problem is that iterators often deal with heap allocated data in the C++ containers and become invalid if the data is independently moved by the containers. Functions that change the size of the container often invalidate all iterators pointing to it, creating dangerous cases of undefined behavior. Here is an example where the iterators in the for loop get invalidated because of the std::string container changing its size on the heap:

  1. include
  2. include
int main

Uniform initialization syntax

The C++11 uniform initialization syntax and std::initializer_list share the same syntax which are triggered differently depending on the internal workings of the classes. If there is a std::initializer_list constructor then this is called. Otherwise the normal constructors are called with the uniform initialization syntax. This can be confusing for beginners and experts alike

  1. include
  2. include
int main

Exceptions

There have been concerns that the zero-overhead principle isn't compatible with exceptions. Most modern implementations have a zero performance overhead when exceptions are enabled but not used, but do have an overhead during exception handling and in binary size due to the need to unroll tables. Many compilers support disabling exceptions from the language to save the binary overhead. Exceptions have also been criticized for being unsafe for state-handling. This safety issue has led to the invention of the RAII idiom, which has proven useful beyond making C++ exceptions safe.

Encoding of string literals in source-code

C++ string literals, like those of C, do not consider the character encoding of the text within them: they are merely a sequence of bytes, and the C++ string class follows the same principle. Although source code can request an encoding for a literal, the compiler does not attempt to validate that the chosen encoding of the source literal is "correct" for the bytes being put into it, and the runtime does not enforce character encoding. Programmers who are used to other languages such as Java, Python or C# which try to enforce character encodings often consider this to be a defect of the language.
The example program below illustrates the phenomenon.

  1. include
  2. include
int main

Despite the presence of the C++11 'u8' prefix, meaning "Unicode UTF-8 string literal", the output of this program actually depends on the source file's text encoding. When the source file is encoded using UTF-8, and the output is run on a terminal that's configured to treat its input as UTF-8, the following output is obtained:

byte-count of automatically-chosen, = 22
byte-count of ASCII-only = 18
byte-count of explicit ISO-8859-1 bytes = 18
byte-count of explicit UTF-8 bytes = 22

The output terminal has stripped the invalid UTF-8 bytes from display in the ISO-8859 example string. Passing the program's output through a Hex_dump utility will reveal that they are still present in the program output, and it is the terminal application that removed them.
However, when the same source file is instead saved in ISO-8859-1 and re-compiled, the output of the program on the same terminal becomes:

byte-count of automatically-chosen, = 18
byte-count of ASCII-only = 18
byte-count of explicit ISO-8859-1 bytes = 18
byte-count of explicit UTF-8 bytes = 22

Code bloat

Some older implementations of C++ have been accused of generating code bloat.

Works cited

*