P.
J.
Plauger's
The C++ Standard Template Library
is an annotated implementation
of the STL. Who's he to annotate the STL? Plauger has been involved in
the STL implementation that comes with the Microsoft Visual C++ 6.0 compiler
and the STL that is offered by
Dinkumware
Ltd.
, of which he is the president. He's also a veteran programmer
who has been involved in the standardization of the standard C library
as well as the standard C++ library.
Some
readers may know his earlier books:
The
Standard C Library
and
The
Draft Standard C++ Library
. All his books are devoted to the implementation
of a standard library. They present the complete source code of the entire
library, together with explanations of principles and details of the implementation.
Not surprisingly, the source code provided in this book is based on the
work of the inventors of the STL, namely Alexander Stepanov, Meng Lee,
and David Musser, who appear as coauthors of
The C++ Standard Template
Library
. Still, regarding the style and content, this book is all Plauger's.
The
book's contents is fairly predictable. There are 14 chapters, one for each
header file in the STL. Each chapter falls into six sections:
-
Some information
on the background of the respective component
-
A functional
description, which provides pretty much what you would expect to find in
a manual
-
Some guidelines
for using the component
-
The source
code of an implementation of the component
-
The source
code of a rudimentary test suite
-
Some exercises
(without providing solutions)
As you
can probably tell, this book is dry, hard to digest, and not the least
bit entertaining. If you're a computer nerd who considers reading C++ source
code an utterly delightful spare-time activity, then you may truly enjoy
reading this book. For the average human reader, the book is useful mainly
as a reference, definitely not as cover-to-cover read.
If
you are interested in background information and deeper insights into the
mechanics of the STL,
The C++ Standard Template Library
is truly
worth having because it explains lots of implementation details that you
won't find mentioned anyplace else.
Here
are a couple of examples:
-
The book
explains the memory allocation strategy of a vector when it must reallocate
its internal memory because insert() is called and the capacity is already
exhausted. The C++ Standard does not say anything about the allocation
strategy of any of the containers, and STL manuals often do not do so either.
Yet it is a detail that might be of interest to STL users in order to make
reliable estimates of the overall cost of performing certain operations
on a vector.
-
Another
interesting detail is the information given about the internal organization
of a "deque" or the tree-based containers "set" and "map." In contrast
to most STL books, which usually only scratch the surface of these topics
and don't delve into the details of the underlying implementation, this
book provides wonderful background information that helps you understand
where the complexity guarantees in the STL stem from and how they are met
by an implementation.
-
The most
delightful section for us is the list of additional requirements imposed
by the author's STL implementation on allocators supplied to the containers.
(See, for instance, the section about the "list" container and its allocator.)
The C++ Standard has little to say about requirements to an allocator.
Although there is a table of requirements in the Standard, the requirements
specified for both containers and allocators are very weak, by and large,
and are close to insufficient. The Standard does not even require that
a container implementation use the allocator for allocating
all
data that belongs to a container; it only requires that the container elements
must be allocated and deallocated via the container's allocator.
In
a list container, for instance, you would expect that not only the container
element themselves, but also the links between the elements, are allocated
via the container's allocator. What is the point of providing a user-defined
allocator for thread-local memory if half the container is allocated under
the allocator's regime and the rest is allocated elsewhere?e.g., in an
implementation-defined memory location? This obvious omission from the
Standard has been around since allocators were added to the STL. Of course,
every implementer is free to do more than the Standard requires, and this
STL implementation does what is sensible. The book explains how it's done,
it provides great insights into the use of allocators by container, and
it will help you if you ever consider providing your own user-defined allocator.
Interesting
as all these glances behind the curtain and under the hood are, you should
never forget that much of the information given here is implementation-specific.
There is no guarantee whatsoever that another implementation of the STL
uses the same strategies as described in this book. In some places Plauger
points out deviations from the C++ Standard, but not always.
For
instance, workarounds that were introduced to make up for compiler deficiencies
are clearly marked as deviations. A counter-example is the unique() algorithm.
The book describes and shows a unique() algorithm that does not comply
to the Standard. The Standard says that unique() calls the predicate
exactly
"(last-first)-1" times. This book says that unique() calls the predicate
at
most
"last-first" times. That's a correct description of the unique()
algorithm shown in this book, but not of the unique() algorithm specified
in the C++ Standard.
In
fairness, we should add that there is an open issue on the Standards Committee
regarding unique(). This book does not even mention that a certain design
decision was made by the author and that the implementation he shows in
the book does not meet the requirements as they are defined by the current
C++ Standard. There are similar deviations from the Standard in other places
that are not marked as such.
Another
issue we have with Plauger's book is that the index does not contain any
references to the source code. Half the book is source code and none of
it is referenced in the index. As a result, it is extremely difficult to
find the actual C++ code of a given function when you need to look it up.
The editor should have fixed this unforgivable flaw.
Make
sure you have the right expectations when you open this book. Don't expect
an introduction to C++ or the STL; you should already be fluent in both.
Don't expect to find an STL tutorial either; this book does not aim to
show you how to use the it or to explain its design. If you take
The
C++ Standard Template Library
for what it is?an annotated STL implementation?and
you are interested in implementing its techniques and details, then this
is a useful resource. In fact, it might be particularly useful for users
of the MVC 6.0 compiler because the MVC STL is close to cryptic, while
the source code in this book, which is relatively similar to the MVC STL,
is far more readable.
Angelika
Langer
develops and teaches classes on Java, C++, multithreading, and
internationalization. She is an internationally recognized speaker and
served on the ANSI/ISO C++ Committee from 1993 to 1998.
Klaus Kreft
is a software architect and senior consultant with 15+ years of experience
in industrial software development. He currently works for Siemens Business
Services in Germany. Langer and Kreft are authors of "Standard C++ IOStreams
and Locales" (Addison-Wesley, 2000) and are columnists for the C/C++ Users
Journal.
|