1.2 Overall Structure of the Book
Our goal is to provide the information necessary for starting to use templates and benefit from their power, as well as to provide information that will enable
Each of these parts consists of several chapters. In addition, we provide a few appendixes that cover material not exclusively related to templates (for example, an overview of overload resolution in C++).
The chapters of Part I are
Last, we provide a rather complete index that encourages additional ways to read this book out of sequence.
1.3 How to Read This Book
If you are a C++ programmer who wants to learn or review the concepts of templates,
Depending on your preferred learning method, you may decide to
The appendixes contain much useful information that is often referred to in the main text. We have also tried to make them interesting in their own right.
In our experience, the best way to learn something new is to look at examples. Therefore, you'll find a lot of examples throughout the book. Some are just a few lines of code illustrating an abstract concept, whereas others are complete programs that provide a concrete application of the material. The latter kind of examples will be introduced by a C++ comment describing the file containing the program code. You can find these files at the Web site of this book at http://www.josuttis.com/tmplbook/.
1.4 Some Remarks About Programming Style
C++ programmers use different programming styles, and so do we: The usual questions about where to put whitespace, delimiters (braces, parentheses), and so forth came up. We tried to be consistent in general, although we occasionally make
We do want to draw your attention to one slightly uncommon decision regarding the declaration of types, parameters, and
void foo (const int &x); void foo (const int& x); void foo (int const &x); void foo (int const& x);
Although it is a bit less common, we decided to use the order int const rather than const int for "constant integer." We have two reasons for this. First, it provides for an easier answer to the question, " What is constant?" It's always what is in front of the const qualifier. Indeed, although
const int N = 100;
is equivalent to
int const N = 100;
there is no equivalent form for
int* const bookmark; // the pointer cannot change, but the // value pointed to can change
that would place the const qualifier before the pointer operator * . In this example, it is the pointer itself that is constant, not the int to which it points.
Our second reason has to do with a syntactical substitution principle that is very common when dealing with templates. Consider the following two type definitions  :
typedef char* CHARS; typedef CHARS const CPTR; // constant pointer to char s
The meaning of the second declaration is preserved when we textually replace CHARS with what it stands for:
typedef char* const CPTR; // constant pointer to char s
However, if we write const before the type it qualifies, this principle doesn't apply. Indeed, consider the alternative to our first two type definitions presented earlier:
typedef char* CHARS; typedef const CHARS CPTR; // constant pointer to char s
Textually replacing CHARS results in a type with a different meaning:
typedef const char* CPTR; // pointer to constant char s
The same observation applies to the
Regarding whitespaces, we decided to put the space between the ampersand and the parameter
void foo (int const& x);
By doing this, we
char* a, b;
where, according to the rules inherited from C, a is a pointer but b is an ordinary char . To avoid such confusion, we simply avoid declaring multiple entities in this way.
This is not a book about the C++ standard library, but we do make use of that library in some of our examples. In general, we use the C++-specific headers (for example,