I was inquisitive 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 initialize 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.
Modernes C++ Mentoring
Be part of my mentoring programs:
Do you want to stay informed about my mentoring programs: Subscribe via E-Mail.
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.
Arguing against this myth is tricky and, in general, impossible for the C++ standard library. This is why I make only a few observations at 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 of 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 the use of C++ in applications where performance matters, and
- to present techniques for implementing C++ Standard language and library facilities to yield efficient code.
With more than 200 pages, the paper's authors 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 systems, 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 optimization levels. I will give you only an idea of the results that are pretty 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 performance from their C pedant.
- The runtime executed dynamic_cast has some overhead. (Remark: The conversion has no C pendant.).
- 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 a free function with the help of a pointer 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
- It can cause time and 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 relatively slow. That seems to be 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 must move and manage additional data structures to deal 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 runtime. (Remarks: That statements hold only if no exceptions were thrown.). The table strategy is more challenging 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 specialization helps to reduce template instantiations.
You can read the details, the exact number, and a few additional topics directly in the report: TR18015.pdf.
Still not convinced? Here is a citation from MISRA C++:
MISRA C++ (Motor Industry Software Reliability Association) formulates guidelines for software in safety-critical systems. Initially designed for the automotive industry, it became the de facto standard in the aviation, military, and medical sectors. The question is, what MISRA says about C++?
MISRA emphasizes 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 slight 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 holds not only for MISRA C++ but also for the previously mentioned Technical report on C++ performance.
I can not conclude my post without a few observations of modern C++. Modern C++ is primarily 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, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Animus24, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, and Rob North.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, and Slavko Radman.
My special thanks to Embarcadero
My special thanks to PVS-Studio
My special thanks to Tipi.build
My special thanks to Take Up Code
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.
- C++ - The Core Language
- C++ - The Standard Library
- C++ - Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org