|
T
his
is the third book by Scott Meyers about effective use of the C++ programming
language and it can easily keep up with the high standards of its reputable
predecessors,
Effective
C++
and
More Effective C++
. This time the focus is not on
the language itself but on a part of the standard C++ library known as
the STL. Like its predecessors,
Effective STL: 50 Specific Ways to Improve
Your Use of the Standard Template Library
discusses each potential
pitfall and gives (sometimes entertaining) guidelines for avoiding the
trap.
Meyers
covers all corners of the STL: containers, iterators, algorithms, and functors.
Even allocators are mentioned as well as common extensions to the STL,
such as hash-based containers and smart pointers. To give you an idea of
the topics discussed in
Effective STL
, let's look at some of its
guidelines:
-
Item
30: Make sure destination ranges are big enough.
This is commonsensical,
yet every STL novice makes exactly this mistake at least once by creating
an empty vector and passing its begin iterator as an output iterator to
an algorithm. The result is a program crash. From then on novices usually
know that they must use insert iterators instead.
-
Item
32: Follow "remove"-like algorithms by "erase" if you really want to remove
something.
The "remove"-like algorithms are notorious misnomers in
the STL because "remove" does not remove anything; it just copies all valid
elements to the beginning of the sequence and returns an iterator to the
garbage at the end. Anybody who has ever used a "remove" algorithm has
noticed it; it's described in every good STL book.
As you
can see, some of
Effective STL
's guidelines are common sense, while
others are well-known to practitioners who have been using the STL. There
are also some items that cover rare and exotic situations.
The
most amusing item is Item 6, which explains "C++'s most vexing parse."
This has actually nothing to do with the STL itself; it is a C++ language
feature. However, it's mentioned in this book because most people encounter
this problem for the first time in their life when they start using the
STL. The problem is that under certain circumstances the C++ compiler interprets
an object declaration as a function declaration. It's a problem similar
to the following: when a programmer says
T t();
then he usually
intends to default-construct an object
t
of type
T
. This
would be true in Java, but in C++ it means that the programmer declares
a function
t
that takes no arguments and returns an object of type
T
.
Item 6 explains a similar, yet more complex case that comes up when you
pass unnamed temporaries as arguments to constructors, which is absolutely
common in conjunction with the STL. The resulting compiler diagnostics
are devastating. If you want to know more, read the book.
By
and large
Effective STL: 50 Specific Ways to Improve Your Use of the
Standard Template Library
is a very sound and readable book; at times
it is downright entertaining. It is worth reading after some exposition
to the STL and is certainly not meant as an introduction to the STL. Much
of its content might be rather obvious if you're an expert user of the
STL. But then, who is? For the inquisitive STL user, this book will definitely
be enlightening and fun to read.
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.
|