C++ Common Knowledge: Essential Intermediate Programming

What Every Professional C++ Programmer Needs to Know—Pared to Its Essentials So It Can Be Efficiently and Accurately Absorbed

 

C++ is a large, complex language, and learning it is never entirely easy. But some concepts and techniques must be thoroughly mastered if programmers are ever to do professional-quality work. This book cuts through the technical details to reveal what is commonly understood to be absolutely essential. In one slim volume, Steve Dewhurst distills what he and other experienced managers, trainers, and authors have found to be the most critical knowledge required for successful C++ programming. It doesn’t matter where or when you first learned C++. Before you take another step, use this book as your guide to make sure you’ve got it right!

 

This book is for you if

  • You’re no “dummy,” and you need to get quickly up to speed in intermediate to advanced C++
  • You’ve had some experience in C++ programming, but reading intermediate and advanced C++ books is slow-going
  • You’ve had an introductory C++ course, but you’ve found that you still can’t follow your colleagues when they’re describing their C++ designs and code
  • You’re an experienced C or Java programmer, but you don’t yet have the experience to develop nuanced C++ code and designs
  • You’re a C++ expert, and you’re looking for an alternative to answering the same questions from your less-experienced colleagues over and over again

C++ Common Knowledge covers essential but commonly misunderstood topics in C++ programming and design while filtering out needless complexity in the discussion of each topic. What remains is a clear distillation of the essentials required for production C++ programming, presented in the author’s trademark incisive, engaging style.

"1125543357"
C++ Common Knowledge: Essential Intermediate Programming

What Every Professional C++ Programmer Needs to Know—Pared to Its Essentials So It Can Be Efficiently and Accurately Absorbed

 

C++ is a large, complex language, and learning it is never entirely easy. But some concepts and techniques must be thoroughly mastered if programmers are ever to do professional-quality work. This book cuts through the technical details to reveal what is commonly understood to be absolutely essential. In one slim volume, Steve Dewhurst distills what he and other experienced managers, trainers, and authors have found to be the most critical knowledge required for successful C++ programming. It doesn’t matter where or when you first learned C++. Before you take another step, use this book as your guide to make sure you’ve got it right!

 

This book is for you if

  • You’re no “dummy,” and you need to get quickly up to speed in intermediate to advanced C++
  • You’ve had some experience in C++ programming, but reading intermediate and advanced C++ books is slow-going
  • You’ve had an introductory C++ course, but you’ve found that you still can’t follow your colleagues when they’re describing their C++ designs and code
  • You’re an experienced C or Java programmer, but you don’t yet have the experience to develop nuanced C++ code and designs
  • You’re a C++ expert, and you’re looking for an alternative to answering the same questions from your less-experienced colleagues over and over again

C++ Common Knowledge covers essential but commonly misunderstood topics in C++ programming and design while filtering out needless complexity in the discussion of each topic. What remains is a clear distillation of the essentials required for production C++ programming, presented in the author’s trademark incisive, engaging style.

32.49 In Stock
C++ Common Knowledge: Essential Intermediate Programming

C++ Common Knowledge: Essential Intermediate Programming

by Stephen Dewhurst
C++ Common Knowledge: Essential Intermediate Programming

C++ Common Knowledge: Essential Intermediate Programming

by Stephen Dewhurst

eBook

$32.49  $42.99 Save 24% Current price is $32.49, Original price is $42.99. You Save 24%.

Available on Compatible NOOK devices, the free NOOK App and in My Digital Library.
WANT A NOOK?  Explore Now

Related collections and offers


Overview

What Every Professional C++ Programmer Needs to Know—Pared to Its Essentials So It Can Be Efficiently and Accurately Absorbed

 

C++ is a large, complex language, and learning it is never entirely easy. But some concepts and techniques must be thoroughly mastered if programmers are ever to do professional-quality work. This book cuts through the technical details to reveal what is commonly understood to be absolutely essential. In one slim volume, Steve Dewhurst distills what he and other experienced managers, trainers, and authors have found to be the most critical knowledge required for successful C++ programming. It doesn’t matter where or when you first learned C++. Before you take another step, use this book as your guide to make sure you’ve got it right!

 

This book is for you if

  • You’re no “dummy,” and you need to get quickly up to speed in intermediate to advanced C++
  • You’ve had some experience in C++ programming, but reading intermediate and advanced C++ books is slow-going
  • You’ve had an introductory C++ course, but you’ve found that you still can’t follow your colleagues when they’re describing their C++ designs and code
  • You’re an experienced C or Java programmer, but you don’t yet have the experience to develop nuanced C++ code and designs
  • You’re a C++ expert, and you’re looking for an alternative to answering the same questions from your less-experienced colleagues over and over again

C++ Common Knowledge covers essential but commonly misunderstood topics in C++ programming and design while filtering out needless complexity in the discussion of each topic. What remains is a clear distillation of the essentials required for production C++ programming, presented in the author’s trademark incisive, engaging style.


Product Details

ISBN-13: 9780672333668
Publisher: Pearson Education
Publication date: 02/28/2005
Sold by: Barnes & Noble
Format: eBook
Pages: 272
File size: 6 MB
Age Range: 18 Years

About the Author

Stephen C. Dewhurst was among the first users of C++ at Bell Labs. He has more than twenty years of experience in applying C++ to problem areas such as compiler design, securities trading, e-commerce, and embedded telecommunications. He is the author and coauthor of several books on C++ and is a member of the advisory board for The C++ Source, a contributing editor for C/C++ Users Journal, and a former columnist for C++ Report. He is also the author of two C++ compilers and numerous articles on compiler design and C++ programming techniques.

Read an Excerpt

A successful book is not made of what is in it, but what is left out of it.
—Mark Twain...as simple as possible, but no simpler.
—Albert Einstein...a writer who questions the capacity of the person at the other end of the line is not a writer at all, merely a schemer.
—E.B. White

When he took over the editorship of the late C++ Report, the quick Herb Sutter asked me to write a column on a topic of my choosing. I agreed, and I chose to call the column “Common Knowledge.” It was supposed to be, in Herb’s words, “a regular summary of basic lore that every working C++ programmer should know—but can’t always.” After a couple of columns in that vein, however, I became interested in template metaprogramming techniques, and the topics treated in “Common Knowledge” from that point on were far from common.

However, the problem in the C++ programming industry that motivated my original choice of column remains. I commonly encounter the following types of individuals in my training and consulting work:

  • Domain experts who are expert C programmers but who have only basic knowledge of (and perhaps some animosity toward) C++
  • Talented new hires direct from university who have an academic appreciation for the C++ language but little production C++ experience
  • Expert Java programmers who have little C++ experience and who have a tendency to program in C++ the way one would program in Java
  • C++ programmers with several years of experience maintaining existing C++ applications but who have not been challenged to learn anything beyond the basics required for maintenance

I want to be immediately productive, but many of the people with whom I’m working or who I’m training require preliminary education in various C++ language features, patterns, and coding techniques before we can get down to business. Worse, I suspect that most C++ code is written in ignorance of at least some of these basics and is therefore not what most C++ experts would consider to be production quality.

This book addresses this pervasive problem by providing essential, common knowledge that every professional C++ programmer needs to know, in a form that is pared to its essentials and that can be efficiently and accurately absorbed. Much of the information is already available from other sources or is part of that compendium of unwritten information that all expert C++ programmers know. The advantage is that this material resides in one place and was selected according to what my training and consulting experience over many years has shown are the most commonly misunderstood and most useful language features, concepts, and techniques.

Perhaps the most important aspect of the sixty-three short items that make up this book is what they leave out, rather than what they contain. Many of these topics have the potential to become complex. An author’s ignorance of these complexities could result in an uninformed description that could mislead the reader, but an expert discussion of a topic in its full complexity could inundate the reader. The approach used here is to filter out needless complexity in the discussion of each topic. What remains, I hope, is a clear distillation of the essentials required for production C++ programming. C++ language wonks will recognize, therefore, that I’ve left out discussion of some issues that are interesting and even important from a theoretical perspective, but the ignorance of which does not commonly affect one’s ability to read and write production C++ code.

Another motivation for this book came as I was engaged in conversation with a group of well-known C++ experts at a conference. There was a general pall or depression among these experts that modern C++ is so complex that the “average” programmer can no longer understand it. (The specific issue was name binding in the context of templates and namespaces. Yes, getting worked up about such a topic does imply the need for more play with normal children.) On reflection, I’d have to say our attitude was pretentious and our gloom unwarranted. We “experts” have no such problems, and it’s as easy to program in C++ as it is to speak a (vastly more complex) natural language, even if you can’t diagram the deep structure of your every utterance. A recurring theme of this book is that while the full description of the minutia of a particular language feature may be daunting, day-to-day use of the feature is straightforward and natural.

Consider function overloading. A full description occupies a large chunk of the standard and whole or multiple chapters in many C++ texts. And yet, when faced with

void f( int );
void f( const char
• );
//...
f( "Hello" );

not a single practicing C++ programmer will be unable to determine which f is called. Full knowledge of the rules by which a call to an overloaded function is resolved is useful but only rarely necessary. The same applies to many other ostensibly complex areas of C++ language and idiom.

This is not to say that all the material presented here is easy; it’s “as simple as possible, but no simpler.” In C++ programming, as in any other worthwhile intellectual activity, many important details can’t be written on an index card. Moreover, this is not a book for “dummies.” I feel a great deal of responsibility to those who grant a portion of their valuable time to reading my books. I respect these readers and try to communicate with them as I would in person to any of my colleagues. Writing at an eighth-grade level to a professional isn’t writing. It’s pandering.

Many of the book’s items treat simple misunderstandings that I’ve seen over and over again, which just need to be pointed out (for example, scope order for member function lookup and the difference between overriding and overloading). Others deal with topics that are in the process of becoming essential knowledge for C++ professionals but are often incorrectly assumed to be difficult and are avoided (for example, class template partial specialization and template template parameters). I’ve received some criticism from the expert reviewers of the manuscript that I’ve spent too much space (approximately one third of the book) on template issues that are not really common knowledge. However, each of these experts pointed out one, two, or several of the template topics they thought did belong in the book. The telling observation is, I think, that there was little overlap among these suggestions, and every template-related item had at least one supporter.

This is the crux of the issue with the items that make up this book. I don’t expect any reader to be ignorant of every item’s topic, and it’s likely that some readers will be familiar with all of them. Obviously, if a reader is not familiar with a particular topic, there would be (I presume) some benefit in reading about it. However, even if a reader is already familiar with a topic, I’d hope that reading about it from a new perspective might clear up a slight misunderstanding or lead to a deeper understanding. This book may also have a role in saving the more experienced C++ programmer precious time. Competent C++ programmers often find themselves (as described previously) answering the same questions over and over again to the detriment of their own work. I’d suggest that the approach of “read this first, and then let’s talk” would save these C++ gurus countless hours and direct their expertise instead to the complex problems for which it’s really needed.

I initially tried to group these sixty-three items into neat chapters, but the items had other ideas. They instead tended to clump themselves together in ways that ranged from the obvious to the unexpected. For example, the items related to exceptions and resource management form a rather natural group. Less obviously, the items Capability Queries, Meaning of Pointer Comparison, Virtual Constructors and Prototype, Factory Method, and Covariant Return Types are strongly and somewhat surprisingly interrelated and are best grouped in close proximity to each other. Pointer Arithmetic decided to hang with Smart Pointers rather than with the pointer and array material earlier in the book. Rather than attempt to impose an arbitrary chapter structure on these natural groupings, I decided to grant the individual items freedom of association. Of course, many other interrelationships exist among the topics treated by the items than can be represented in a simple linear ordering, so the items make frequent internal references among themselves. It’s a clumped but connected community.

While the main idea is to be brief, discussion of a topic sometimes includes ancillary details that are not directly related to the subject at hand. These details are never necessary to follow the discussion, but the reader is put on notice that a particular facility or technique exists. For instance, the Heap template example that appears in several items informs the reader in passing about the existence of the useful but rarely discussed STL heap algorithms, and the discussion of placement new outlines the technical basis of the sophisticated buffer management techniques employed by much of the standard library. I also try to take the opportunity, whenever it seems natural to do so, to fold the discussion of subsidiary topics into the discussion of a particular, named item. Therefore, RAII contains a short discussion of the order of constructor and destructor activation, Template Argument Deduction discusses the use of helper functions for specializing class templates, and Assignment and Initialization Are Different folds in a discussion of computational constructors. This book could easily have twice the number of items, but, like the clumping of the items themselves, correlation of a subsidiary topic with a specific item puts the topic in context and helps the reader to absorb the material efficiently and accurately.

I’ve reluctantly included several topics that cannot reasonably be treated in this book’s format of short items. In particular, the items on design patterns and the design of the standard template library are laughably short and incomplete. Yet they make an appearance simply to put some common misconceptions to rest, emphasize the importance of the topics, and encourage the reader to learn more.

Stock examples are part of our programming culture, like the stories that families swap when they gather for holidays. Therefore, Shape, String, Stack, and many of the other usual suspects put in an appearance. The common appreciation of these baseline examples confers the same efficiencies as design patterns in communication, as in “Suppose I want to rotate a Shape, except...” or “When you concatenate two Strings...” Simply mentioning a common example orients the conversation and avoids the need for time-consuming background discussion. “You know how your brother acts whenever he’s arrested? Well, the other day...”

Unlike my previous books, this one tries to avoid passing judgment on certain poor programming practices and misuses of C++ language features; that’s a goal for other books, the best of which I list in the bibliography. (I was, however, not entirely successful in avoiding the tendency to preach; some bad programming practices just have to be mentioned, even if only in passing.) The goal of this book is to inform the reader of the technical essentials of production-level C++ programming in as efficient a manner as possible.

—Stephen C. Dewhurst Carver, Massachusetts January 2005

Table of Contents

Preface xi
Acknowledgments xvii
A Note on Typographical Conventions xix

Item 1:Data Abstraction 1
Item 2:Polymorphism 3
Item 3:Design Patterns 7
Item 4:The Standard Template Library 11
Item 5:References Are Aliases, Not Pointers 13
Item 6:Array Formal Arguments 17
Item 7:Const Pointers and Pointers to Const 21
Item 8:Pointers to Pointers 25
Item 9:New Cast Operators 29
Item 10:Meaning of a Const Member Function 33
Item 11:The Compiler Puts Stuff in Classes 37
Item 12:Assignment and Initialization Are Different 41
Item 13:Copy Operations 45
Item 14:Function Pointers 49
Item 15:Pointers to Class Members Are Not Pointers 53
Item 16:Pointers to Member Functions Are Not Pointers 57
Item 17:Dealing with Function and Array Declarators 61
Item 18:Function Objects 63
Item 19:Commands and Hollywood 67
Item 20:STL Function Objects 71
Item 21:Overloading and Overriding Are Different 75
Item 22:Template Method 77
Item 23:Namespaces 81
Item 24:Member Function Lookup 87
Item 25:Argument Dependent Lookup 89
Item 26:Operator Function Lookup 91
Item 27:Capability Queries 93
Item 28:Meaning of Pointer Comparison 97
Item 29:Virtual Constructors and Prototype 99
Item 30:Factory Method 103
Item 31:Covariant Return Types 107
Item 32:Preventing Copying 111
Item 33:Manufacturing Abstract Bases 113
Item 34:Restricting Heap Allocation 117
Item 35:Placement New 119
Item 36:Class-Specific Memory Management 123
Item 37:Array Allocation 127
Item 38:Exception Safety Axioms 131
Item 39:Exception Safe Functions 135
Item 40:RAII 139
Item 41:New, Constructors, and Exceptions 143
Item 42:Smart Pointers 145
Item 43:auto_ptr Is Unusual 147
Item 44:Pointer Arithmetic 149
Item 45:Template Terminology 153
Item 46:Class Template Explicit Specialization 155
Item 47:Template Partial Specialization 161
Item 48:Class Template Member Specialization 165
Item 49:Disambiguating with Typename 169
Item 50:Member Templates 173
Item 51:Disambiguating with Template 179
Item 52:Specializing for Type Information 183
Item 53:Embedded Type Information 189
Item 54:Traits 193
Item 55:Template Template Parameters 199
Item 56:Policies 205
Item 57:Template Argument Deduction 209
Item 58:Overloading Function Templates 213
Item 59:SFINAE 217
Item 60:Generic Algorithms 221
Item 61:You Instantiate What You Use 225
Item 62:Include Guards 229
Item 63:Optional Keywords 231

Bibliography 235
Index 237
Index of Code Examples 245

Preface

A successful book is not made of what is in it, but what is left out of it.
—Mark Twain
...as simple as possible, but no simpler.
—Albert Einstein
...a writer who questions the capacity of the person at the other end of the line is not a writer at all, merely a schemer.
—E.B. White

When he took over the editorship of the late C++ Report, the quick Herb Sutter asked me to write a column on a topic of my choosing. I agreed, and I chose to call the column “Common Knowledge.” It was supposed to be, in Herb’s words, “a regular summary of basic lore that every working C++ programmer should know—but can’t always.” After a couple of columns in that vein, however, I became interested in template metaprogramming techniques, and the topics treated in “Common Knowledge” from that point on were far from common.

However, the problem in the C++ programming industry that motivated my original choice of column remains. I commonly encounter the following types of individuals in my training and consulting work:

  • Domain experts who are expert C programmers but who have only basic knowledge of (and perhaps some animosity toward) C++
  • Talented new hires direct from university who have an academic appreciation for the C++ language but little production C++ experience
  • Expert Java programmers who have little C++ experience and who have a tendency to program in C++ the way one would program in Java
  • C++ programmers with several years of experience maintaining existing C++ applications but who have not been challenged to learn anything beyond the basics required for maintenance

I want to be immediately productive, but many of the people with whom I’m working or who I’m training require preliminary education in various C++ language features, patterns, and coding techniques before we can get down to business. Worse, I suspect that most C++ code is written in ignorance of at least some of these basics and is therefore not what most C++ experts would consider to be production quality.

This book addresses this pervasive problem by providing essential, common knowledge that every professional C++ programmer needs to know, in a form that is pared to its essentials and that can be efficiently and accurately absorbed. Much of the information is already available from other sources or is part of that compendium of unwritten information that all expert C++ programmers know. The advantage is that this material resides in one place and was selected according to what my training and consulting experience over many years has shown are the most commonly misunderstood and most useful language features, concepts, and techniques.

Perhaps the most important aspect of the sixty-three short items that make up this book is what they leave out, rather than what they contain. Many of these topics have the potential to become complex. An author’s ignorance of these complexities could result in an uninformed description that could mislead the reader, but an expert discussion of a topic in its full complexity could inundate the reader. The approach used here is to filter out needless complexity in the discussion of each topic. What remains, I hope, is a clear distillation of the essentials required for production C++ programming. C++ language wonks will recognize, therefore, that I’ve left out discussion of some issues that are interesting and even important from a theoretical perspective, but the ignorance of which does not commonly affect one’s ability to read and write production C++ code.

Another motivation for this book came as I was engaged in conversation with a group of well-known C++ experts at a conference. There was a general pall or depression among these experts that modern C++ is so complex that the “average” programmer can no longer understand it. (The specific issue was name binding in the context of templates and namespaces. Yes, getting worked up about such a topic does imply the need for more play with normal children.) On reflection, I’d have to say our attitude was pretentious and our gloom unwarranted. We “experts” have no such problems, and it’s as easy to program in C++ as it is to speak a (vastly more complex) natural language, even if you can’t diagram the deep structure of your every utterance. A recurring theme of this book is that while the full description of the minutia of a particular language feature may be daunting, day-to-day use of the feature is straightforward and natural.

Consider function overloading. A full description occupies a large chunk of the standard and whole or multiple chapters in many C++ texts. And yet, when faced with

void f( int );
void f( const char * );
//...
f( "Hello" );

not a single practicing C++ programmer will be unable to determine which f is called. Full knowledge of the rules by which a call to an overloaded function is resolved is useful but only rarely necessary. The same applies to many other ostensibly complex areas of C++ language and idiom.

This is not to say that all the material presented here is easy; it’s “as simple as possible, but no simpler.” In C++ programming, as in any other worthwhile intellectual activity, many important details can’t be written on an index card. Moreover, this is not a book for “dummies.” I feel a great deal of responsibility to those who grant a portion of their valuable time to reading my books. I respect these readers and try to communicate with them as I would in person to any of my colleagues. Writing at an eighth-grade level to a professional isn’t writing. It’s pandering.

Many of the book’s items treat simple misunderstandings that I’ve seen over and over again, which just need to be pointed out (for example, scope order for member function lookup and the difference between overriding and overloading). Others deal with topics that are in the process of becoming essential knowledge for C++ professionals but are often incorrectly assumed to be difficult and are avoided (for example, class template partial specialization and template template parameters). I’ve received some criticism from the expert reviewers of the manuscript that I’ve spent too much space (approximately one third of the book) on template issues that are not really common knowledge. However, each of these experts pointed out one, two, or several of the template topics they thought did belong in the book. The telling observation is, I think, that there was little overlap among these suggestions, and every template-related item had at least one supporter.

This is the crux of the issue with the items that make up this book. I don’t expect any reader to be ignorant of every item’s topic, and it’s likely that some readers will be familiar with all of them. Obviously, if a reader is not familiar with a particular topic, there would be (I presume) some benefit in reading about it. However, even if a reader is already familiar with a topic, I’d hope that reading about it from a new perspective might clear up a slight misunderstanding or lead to a deeper understanding. This book may also have a role in saving the more experienced C++ programmer precious time. Competent C++ programmers often find themselves (as described previously) answering the same questions over and over again to the detriment of their own work. I’d suggest that the approach of “read this first, and then let’s talk” would save these C++ gurus countless hours and direct their expertise instead to the complex problems for which it’s really needed.

I initially tried to group these sixty-three items into neat chapters, but the items had other ideas. They instead tended to clump themselves together in ways that ranged from the obvious to the unexpected. For example, the items related to exceptions and resource management form a rather natural group. Less obviously, the items Capability Queries, Meaning of Pointer Comparison, Virtual Constructors and Prototype, Factory Method, and Covariant Return Types are strongly and somewhat surprisingly interrelated and are best grouped in close proximity to each other. Pointer Arithmetic decided to hang with Smart Pointers rather than with the pointer and array material earlier in the book. Rather than attempt to impose an arbitrary chapter structure on these natural groupings, I decided to grant the individual items freedom of association. Of course, many other interrelationships exist among the topics treated by the items than can be represented in a simple linear ordering, so the items make frequent internal references among themselves. It’s a clumped but connected community.

While the main idea is to be brief, discussion of a topic sometimes includes ancillary details that are not directly related to the subject at hand. These details are never necessary to follow the discussion, but the reader is put on notice that a particular facility or technique exists. For instance, the Heap template example that appears in several items informs the reader in passing about the existence of the useful but rarely discussed STL heap algorithms, and the discussion of placement new outlines the technical basis of the sophisticated buffer management techniques employed by much of the standard library. I also try to take the opportunity, whenever it seems natural to do so, to fold the discussion of subsidiary topics into the discussion of a particular, named item. Therefore, RAII contains a short discussion of the order of constructor and destructor activation, Template Argument Deduction discusses the use of helper functions for specializing class templates, and Assignment and Initialization Are Different folds in a discussion of computational constructors. This book could easily have twice the number of items, but, like the clumping of the items themselves, correlation of a subsidiary topic with a specific item puts the topic in context and helps the reader to absorb the material efficiently and accurately.

I’ve reluctantly included several topics that cannot reasonably be treated in this book’s format of short items. In particular, the items on design patterns and the design of the standard template library are laughably short and incomplete. Yet they make an appearance simply to put some common misconceptions to rest, emphasize the importance of the topics, and encourage the reader to learn more.

Stock examples are part of our programming culture, like the stories that families swap when they gather for holidays. Therefore, Shape, String, Stack, and many of the other usual suspects put in an appearance. The common appreciation of these baseline examples confers the same efficiencies as design patterns in communication, as in “Suppose I want to rotate a Shape, except...” or “When you concatenate two Strings...” Simply mentioning a common example orients the conversation and avoids the need for time-consuming background discussion. “You know how your brother acts whenever he’s arrested? Well, the other day...”

Unlike my previous books, this one tries to avoid passing judgment on certain poor programming practices and misuses of C++ language features; that’s a goal for other books, the best of which I list in the bibliography. (I was, however, not entirely successful in avoiding the tendency to preach; some bad programming practices just have to be mentioned, even if only in passing.) The goal of this book is to inform the reader of the technical essentials of production-level C++ programming in as efficient a manner as possible.

—Stephen C. Dewhurst
Carver, Massachusetts
January 2005

From the B&N Reads Blog

Customer Reviews