The C++ Programming Language

Book Cover

I learned C++ a few years ago and honestly, I havn’t written a single line in over a year. We used Visual C++ 6 in college. For some reason, I felt like learning more about the language details and what it’s really supposed to be. What can be better than a book written by Bjarne Stroustrup, the language’s original author? C++ is a very impressive language. It can be as efficient at very low levels while allowing extreme abstraction. Generic programming and operator overload really is missing in Java. I would say the downside of the language is that, compared to development platforms, it offers very few high level facilities and the standard library, while being very well designed and robust, remains very low level and does not really provide a plus-value in real applications. Of course, there are multiple libraries available around the web to achieve most tasks. Still, searching for a library is additional work.

The thousand-page monster did cover the entire language as I expected it to. The book is divided in 5 main sections: basic facilities, abstraction mechanism, standard library, design using C++ and the appendicies (which are really part of the book). The book tend to get C++ to look very complex as a lot of pages are used to explain the ambiguities, special cases and obscur syntaxes. To avoid any complexity, my recommandation is to keep code clean and don’t abuse those ambiguities, special cases and obscur syntaxes.

Multiple analogies to real world concepts are used to explain technical details and design decisions. Some of them are quite hillarous. Here is one of my favorites. Static and dynamic type checking is compared to electric plugs:

The most obvious thing about these plugs is that they are specifically designed to make it impossible to plug two gadgets together unless the gadgets were designed to be plugged together, and then they can be connected only in the right way. Had you been able to, you would have ended up with a fried shaver or a fried shavee.

The section about the basic facilities introduces the very basic syntax elements such as data types, arrays, structures, namespaces, functions, pointers and such. It also introduces a very important aspect: C++ is a language, not an implementation. Some details are meant to be left for others to decide depending on the target platform or architecture. It’s also made clear that the book won’t cover a single implementation. Which means that if you planned on learning to compile a C++ program, you can simply forget about it and refer to your compiler’s manual.

I was surprised to see that some of the keywords used in the examples were never really defined or very briefly. Those keywords were often for very specific purposes that most programmers would never have to deal with. Still, a list of the keywords and their meaning would have been appreciated. One of the keywords I have in mind is volatile. I’m convinced I saw that keyword somewhere in the first section, but the only reference to it in the index is at page 808 (which is in Appendix A). The paragraph is the following: A volatile specifier is a hint to the compiler that the object may change its value in ways not specified by the language so that aggressive optimizations must be avoided.. While the definition is clear enough, page 808 seems a little far from the first encounter.

The chapter about namespaces is one of the most interesting chapters in the book for some reason. It really demonstrates the flexibility of the namespaces in C++ and explains how to make effective uses of them. In fact, using a global scope makes absolutly no sense.

The abstraction mechanism section introduces the classes and covers them in detail. A basic overview of object oriented programming is made and a few examples are developped through the chapters. Each chapter proposes a different design solution and explains how it could be implemented. The section contains a good explanation of the C++ derivation mechanisms and when they should be used (public, private, protected and their virtual companions don’t have the same purposes). Template classes are explained in detail, which is a good thing since the standard library makes nearly abusive use of them.

An overview of the standard library is made. While nothing really is covered in detail, most concepts are explained and the content of each header file is detailed. In most cases, a situation is explained, followed by an STL solution. The library concepts are then explained and then headers files are detailed. Since the header files contain way too many template definitions, they tend to be cryptic. Using them is usually very simple. Bjarne Stroustroup spends a lot of time explaining all the possible optimizations made in the standard library and that any attempt to produce a better implementation is futile. As much as possible, the standard library should be used over hand-crafted containers.

As I mentionned before, the standard library only provides simple facilities. While C programmers might dream of those at night, I’m not very impressed as I am used to much richer implementations.

The following section totally steps out of technical details and discuss various development-related topics. After 650 pages of technical details, this section was most welcome (STL headers ARE painful to read). The ‘Design Using C++’ section can actually be useful even to non-developpers and even managers. From dealing with developpers to finding the right design, it offers a whole lot more than I would have expected. I have to admit I was not expecting to see any non-technical topics in the book.

The appendicies cover advanced topics that could not fit in the book itself. My edition contains 5 appendicies. Older editions might contain less. The most interesting ones were the one about locale and the one about exception safety.

The book is neither a language specification nor a C++ tutorial. It’s actually closer to an in-depth introduction or a detailed reference (not quick). The major downside is that it’s extremely long. The average chapter is around 35 pages (font is 8pt) and there are 30 of em counting appendices. I sometimes had the feeling too many details were convered while a simple DONT DO THAT would have been enough. I still rate this one 3.5/5.

Leave a Reply

Your email address will not be published.