Edinburgh Castle
C++ in a Nutshell
AuthorsRay Lischner
PublisherO'Reilly
ISBN0-596-00298-X
DateMay 2003
Pages808
Price£28.50
ReviewerRoger Spooner
Cover image for C++ in a Nutshell

This book is concise, readable, and useful. As it makes clear on the cover, this is a reference book, and dives straight into very intense details of the syntax of C++. It is meant as the kind of book you can check when code that you thought should work isn't compiling, or when you want to know the proper way to write something particular. In fact, it reads quite like a plain-English version of the C++ standard itself.

It is perhaps surprising that the order of the chapters matches the abstraction level of each topic; starting with the parsing of characters in a source file, and working up through simple expressions to functions, classes, and finally to the standard libraries provided to assist you in common tasks. This may not be the order that the reader becomes interested in the topics, but it reflects the nature of this book as a reference rather than a cover-to-cover gripping read.

Even that first chapter contains treasures, such as the importance of spaces in nested template declarations, where ">>" would be parsed incorrectly. As well as statements of fact about the language, it includes a few human touches like using words that most of us recognise ("source file", when the standard says "translation unit"), and even advice not to use trigraphs; an obsolete part of the language which is still valid. Later, it warns about the safety of iterators, which can become almost as invalid as pointers, but doesn't spend long on stylistic advice, a bit more of which might be welcome. It dwells more on templates and containers, perhaps because they are less familiar to many readers.

There are lots of short examples of code, particularly in explaining how certain keywords are used and misused. There are cases where something might look like a type or a function; cases where a complicated pointer might be confusing about what it addresses, as well as more familiar cases about the scope of variables that might hide others of the same name. Again, these examples tend to be extremely short and to the point. There are a number of other examples of complete programs, covering a couple of pages, which have several classes with real functionality.

There are a decent number of cross references to other chapters, and some of the same information is repeated in several places; for example the "language reference" (chapter 12) lists every reserved word, with a half page description of its purpose, and refers to related sections that cover the topic in more detail. There were still occasions when it took me longer to find the page I wanted, than I had hoped. Perhaps the style of the index and referencing will become more familiar with use.

Seeing everything together, I was struck by how many innovative features are in the language, and most of them good. While Perl, Python, Java and the other languages we are familiar with have their own strengths (consider, for example, how Perl goes out of its way to let you write the same behaviour in many ways), and admitting that not everything in C++ is good (some people don't use exceptions for fear of ambiguity over resource ownership and clean-up), it was really inspiring to see what C++ has: multiple inheritance, virtual functions, exceptions, templates, dynamic casting, operator overloading, the standard library, namespaces, and so on.

By far the largest part of the book is the standard library reference, which is a good thing. The author is obviously pleased with this, and devotes more than half of the pages to explaining each function. Languages live or die by the capabilities (and elegance) of what comes "in the box". Edinburgh University's ML was an interesting-ish thing to study, but didn't help much when you wanted to interact with the real world. PHP is a cute language for web designers who think they can program, but its library is shockingly haphazard and incomplete. The C++ standard library is remarkable in its wealth of templates for storing data. Containers, iterators, maps, vectors, and the various other methods are covered with delight, encouraging you to get on with programming your new algorithms, instead of reimplementing Donald Knuth's. It also includes all of the C standard library, and various new methods, too.

In summary, this book is a good volume to have on the shelf for those occasions when your C++ compiler answers back about your syntax for subtle language features, and good to have on your desk when you want to use the standard library. Personally, I would have liked a bit more advice about what is efficient, or what is risky. It is very clearly and concisely written, which is a great benefit. Overall, a marked success.

Contents

1. Language Basics
2. Declarations
3. Expressions
4. Statements
5. Functions
6. Classes
7. Templates
8. Standard Library
9. Input and Output
10. Containers, Iterators and Algorithms
11. Preprocessor Reference
12. Language Reference
13. Library Reference
A. Compiler Extensions
B. Projects

faded separator

Page maintained via github.com/edinburgh-pm/edinburgh.pm