HP aC++/HP C Programmer's Guide (B3901-90036; A.06.26; September 2011)
Smaller Source Code
There are approximately 50 different algorithms and about a dozen major data structures.
This separation reduces the source code size, and decreases the risk of similar activities
with dissimilar interfaces. In the absense of this separation, each algorithm must be
implemented in each data structure. This requires additional member functions apart
from those in the present scheme.
Flexibility
The algorithms that are separated from data structures can be used with conventional
C++ pointers and arrays. Algorithms encapsulated within a class hierarchy do not have
this ability because C++ arrays are not objects.
Efficiency
The STL in particular, and the Standard C++ Library in general, provide a low-level
approach to develop C++ applications. This low-level approach is useful when specific
programs require an emphasis on efficient coding and execution speed.
Iterators: Mismatches and Invalidations
The Standard C++ Library data structures use pointer-like objects called iterators to
describe the contents of a container. Given the architecture of the library architecture, it
is impossible to verify whether the iterator elements are matched or derived from the
same container. Using a beginning iterator from one container with an ending iterator
from another produces unexpected results. It is very important to know that iterators are
invalidated as a result of a subsequent insertion or deletion from the underlying container
class. The use of an invalid iterator produces unexpected results. Familiarity with the
Standard C++ Library reduces the number of errors related to iterators.
Templates: Errors and Code Bloat
The flexibility and power of templatized algorithms is, with most compilers, results in a
loss of precision in diagnostics. Errors in the parameter lists of generic algorithms is
sometimes displayed only as obscure compiler errors for internal functions. These errors
are defined many levels deep in template expansions. Familiarity with algorithms and
their requirements is necessary to use the standard library. Heavy reliance on templates
causes programs to grow larger than expected. You can minimize this problem by
recognizing the cost of instantiating a particular template class, and by making
appropriate design decisions. As compilers become more fluent in templates, the problem
of size is reduced.
Multithreading Problems
When you use the Standard C++ Library in a multithreaded environment, the iterators
cannot safely pass between threads. This is because iterators are independent of the
containers they operate on. It is impossible to protect a container when it spawns iterators
in multiple threads as iterators are used to modify a non-const container. Use thread-safe
222 Tools and Libraries