Channels ▼

Andrew Koenig

Dr. Dobb's Bloggers

C++ Primer 5th Edition, Part 1: How To Revise a Textbook

July 19, 2012

Barbara Moo shipped the completed text of the C++ Primer, Fifth Edition to the publisher on July 13. As far as I know, copies are already being printed; they should be on bookstore shelves by mid-August.

This book has been a major project for her for the past two years or so, and an all-consuming one since about the beginning of this year. I've spent a fair amount of time on it as well: reading drafts, making comments and suggestions, running test programs, and so on. As a result, I've had a pretty good idea of what she's been thinking about as she wrote the book, and I'm in a position not only to tell you what I've learned about her strategy, but also why I think her strategy is a sensible one, and one that I think that most books ought to follow — even though I would not be at all surprised to learn that many other books do not do so.

C++11 is intended to be compatible with C++03, which is intended to be compatible with C++98, which is intended to be compatible with C. This lineage of compatibility is a major reason why C++ is so popular and useful. It is tempting, therefore, to think that the way to revise a C++03 textbook is to take advantage of that compatibility by simply adding a section to the book that talks about the new C++11 stuff. Doing so has the advantage of simplicity: Most of the book does not have to change.

From a reader's viewpoint who already knows the old language, adding a section has an advantage too: At least in theory, the reader already knows the old language. Why bother relearning stuff one already knows?

The flaw in this technique is that it makes the new parts of the language into an afterthought — something to be studied only after understanding the rest of the language. And all too often such afterthoughts wind up not being studied or understood at all. For example, it is this phenomenon that leads to people writing C++ programs that are mostly C programs compiled on C++ compilers. In the context of C++11, this phenomenon is apt to lead to programmers writing C++11 programs that are really C++03 programs compiled on C++11 compilers.

Here is a simple example of this phenomenon. Suppose that v is a vector<int> and we want to call a function foo on each element of v. In C++03, we might do so this way:

 
     for (vector<int>::size_type i = 0; i != v.size(); ++i)
           foo(v[i]);

A more sophisticated style is to use an iterator, thereby avoiding what would otherwise be a dependency on the ability to use subscripts:

 
     for (vector<int>::iterator it = v.begin(); it := v.end(); ++it)
           foo(*it);

If we are to write this code fragment to take advantage of C++11, we might well do so differently. For example, we might write the first example this way:

     for (decltype(v.size()) i = 0; i != v.size(); ++i)
           foo(v[i]);

but we would be more likely to write something like this:

 
     for (auto it = v.begin(); it != v.end(); ++it)
           foo(it);

or even like this:

 
     for (auto x: v)
           foo(x)

If a textbook teaches the first two forms, and then mentions the last three forms only as an afterthought, most readers will probably stick to the first two forms and not use the other three at all. In effect, writing a book that is a C++03 book with a C++11 appendix is apt to lead to readers who will write C++03 programs and not bother with C++11. In that case, there is not much reason to write a new book for C++11 at all.

Barbara decided to do it the hard way: Not only discuss the new ideas in C++, but integrate them into the book's organization. Over the next few weeks, I'm going to discuss some examples of how she applied this strategy.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video