I was very curious about your C++ myths. In particular, my German readers were quite active. I got a few E-Mails and observed a vivid discussion on Heise Developer.
Let me first conclude the myths of the C++ core guidelines before I write about your myths. Here is the last myth.
Protected data makes your program complex and error-prone. If you put protected data into a base class, you can not reason about derived classes in isolation and, therefore, you break encapsulation. You always have to reason about the entire class hierarchy.
This means you have to answer at least these three questions.
- Do I have to implement a constructor in a derived class to initialise the protected data?
- What is the actual value of the protected data if I use them?
- Who will be affected if I modify the protected data?
Answering these questions becomes more and more complicated, the deeper your class hierarchy becomes.
If you think about it: protected data is a kind of global data in the scope of the class hierarchy. And you know, a mutable shared state is terrible. It makes, for example, testing and concurrency quite tricky.
Now, I switch to your myths. I write about them in the order I received them.
In C++ written programs need more memory and CPU as in C written programs (Gunter Königsmann)
First of all: thanks to Gunter Königsmann for the classical myth.
Honestly, arguing against this myth is quite difficult and in general not possible for the C++ standard library. This is the reason, I make only a few observations add the end of this chapter. First of all, here are the hard facts. The "Technical Report on C++ Performance" helps me a lot.
The Working Group WG 21 published in the year 2006 the paper ISO/IEC TR 18015. The title sounds not very interesting but that document is the ultimate source if you want to get the performance numbers to the C++ features. The document expresses its concerns directly to the point.
- to give the reader a model of time and space overheads implied by the use of various C++ language and library features,
- to debunk widespread myths about performance problems,
- to present techniques for use of C++ in applications where performance matters, and
- to present techniques for implementing C++ Standard language and library facilities to yield efficient code.
Authors of the paper with more than 200 pages are well known C++ experts like Dave Abrahams, Howard Hinnand, Dietmar Kühl, Dan Saks, Bill Seymour, Bjarne Stroustrup, and Detlef Vollmann.
The scope of the document is C++ features, their overhead and usage, the creation of efficient libraries in C++, the usage of C++ in embedded system, and the interfaces in C++ to communicate with the hardware.
C++ features, Overhead, and Usage
The authors use for their analysis three computer architectures with five different compilers. They use compilers with different optimisation level. I will give you only an idea of the results that are quite remarkable.
- Have no significant overhead in size and performance
- Type converting operator
- The C++ casts const_cast, static_cast, and reinterpret_cast differ neither in size nor in performance from their C pedant.
- The at runtime executed dynamic_cast has some overhead. (Remark: The conversion has no C pedant.).
- A class without virtual functions is as big as a struct.
- A class with virtual functions has the overhead of a pointer and a virtual function table. These are about 2 to 4 bytes.
- Function calls
- The call of a non-virtual, non-static, and non-inline function is as expensive as the call of a free function.
- The call of a virtual function is as expensive as the call of a free function with the help of a pointer that is stored in a table.
- Virtual functions of a class template can cause overhead in size.
- The inlining of a function causes significant performance benefits and is close to the performance of a C macro.
- Multiple inheritances
- Can cause time and/or space overhead.
- Virtual base classes have overhead compared to non-virtual base classes.
- Run-time type information (RTTI)
- There are about 40 additional bytes for each class necessary.
- The typeid call is quite slow. That seems due to the quality of the implementation.
- The conversion during runtime with dynamic_cast is slow. According to the reports that should also be due to the quality of the implementation.
- Exception handling
- There are two strategies for dealing with exceptions. These are the code and the table strategy. The code strategy has to move and manage additional data structures for dealing with exceptions. The table strategy has the execution context in a table.
- The code strategy has a size overhead for the stack and the runtime. The runtime overhead is about 6%. This overhead exists even without the throwing of an exception.
- The table strategy has neither overhand in program size nor in runtime. (Remarks: That statements hold only if no exceptions were thrown.). The table strategy is more difficult to implement.
- You get for each template instantiation a new class template or function template. Therefore, the naive use of temples can cause code bloat. Modern C++ compilers can massively reduce the number of template instantiations. The usage of partial or full specialisation helps to reduce the template instantiations.
You can read the details, the exact number and few additionals topics directly in the report: TR18015.pdf.
Still not convinced? Here is a cite from MISRA C++:
MISRA C++ (Motor Industry Software Reliability Association) formulates guidelines software in safety-critical systems. It was originally designed for the automotive industry and became the de facto standard in the aviation, military and medical sector. The question is, what MISRA says about C++?
MISRA emphasises why the importance of C++ in critical systems becomes more important. (1.1 The use of C++ in critical systems):
- C++ gives good support for high-speed, low-level, input/output operations, which are essential to many embedded systems.
- The increased complexity of applications makes the use of a high-level language more appropriate than assembly language.
- C++ compilers generate code with similar size and RAM requirements to those of C.
But one small downer remains. MISRA C++ is based on classical C++. This is simply speaking C++98. Modern C++ has a lot more to offer for embedded systems. This remark does not only hold for MISRA C++ but also holds on to the previous mentioned Technical report on C++ performance.
I can not conclude my post without a few observations of modern C++. Modern C++ is a term that is mostly used for the three C++ standards C++11, C++14, and C++17.
The difficulty in the comparison of the C++ standard library to its C pendants is, in particular, that you have to compare equivalent data structures or programs. This means, you can not compare a C-string and a C++-string, because the latter supports automatic memory management. The same holds for a C-array in contrast to the C++ containers. Without further ado, here are my points. If you are more curious, read the mentioned post.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, Marko, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, espkk, Louis St-Amour, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Neil Wang, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Red Trip, Alessandro Pezzato, Evangelos Denaxas, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, Eduardo Velasquez, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Haken Gedek, Bernd Mühlhaus, Challanger, Matthieu Bolt, Stephen Kelley, and Kyle Dean.
Thanks in particular to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, Ralf Abramowitsch, and John Nebel.
My special thanks to Embarcadero
My special thanks to PVS-Studio
My new mentoring program "Fundamentals for C++ Professionals" starts in April. Get more information here: https://bit.ly/MentoringProgramModernesCpp.
I'm happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- German Seminar Page: www.ModernesCpp.de
- English Seminar Page: www.ModernesCpp.net