[Effective-cpp] Item 35: Familiarize yourself with the langu age standard

Jaspreet Singh Jasingh at quark.co.in
Tue Jun 10 01:37:00 EDT 2003


Hi All
	in the development projects that i've participated in (although
few), i haven't used any templates and didn't find the need to use them.
ok..the classic swap example, or the one given below in the text can serve
as a good reason to use them but practically i haven't come across such a
situation... probably because the design was such that i bypassed this kind
of a scenario. the ADT programming style is a bit different from the OO
style
i think the "class T" is just a workaround for a parent "Object" class as in
java.
a template can be used whenever we need a family of classes/functions... but
i haven't come across such a situation.

Thanks,
Jaspreet

-----Original Message-----
From: Gregory Haley [mailto:ghaley at venaca.com]
Sent: Tuesday, June 10, 2003 8:06 AM
To: effective-cpp at accu.org
Subject: [Effective-cpp] Item 35: Familiarize yourself with the language
standard


The title of this item is really a tease.  It is really an introduction to
the most salient points about the standard, the standarard library, and in
more depth, the Standard Template Library.

More Effective C++ was completed in 1996, several years before the standard
was adopted.  This item was a preview of what the standard would contain.
The items are bulleted points:

New features:  RTTI, namespaces, bool, and more.
Templates:  member templates, standard syntax for forcing instantiation
Exception handling:  specifications now more rigorously checked
Memory allocation:  operator new[] and operator delete[] have been added
New casting forms: static_cast, dynamic_cast, const_cast, and
reinterpret_cast

However, Meyers points out, the changes to the standard itself pale in
comparison to the changes to the standard library.  He argues that (at the
time of publication) the changes to the library had not been as well
publicized as the changes to the standard, a paucity, I think that has long
ago been readdressed.  In reality, the discussion of the changes to the
library are a lead in to a very brief overview of the STL -- and a nice
segue to the Effective STL project to follow.

The bulleted points about the change to the library include:

Support for the standard C library:  C++ has not forgotten its roots.  The
C++ version brings the C library into conformance with C++'s stricter type
checking.
Support for strings.
Support for localization.
Support for I/O.
Support for numeric applications:  among these is the inclusion of complex
numbers.
Support for general purpose container and algorithms.  Meyers notes, "The
STL is the most revolutionary part of the standard C++ library."

The centrality of the STL derives from the fact that "almost everything in
the library is a template."  As an example, he points out that he has been
discussing the 'string' class, when in reality there is no such class, but
rather "a class template called basic_string that represents sequences of
characters."  Indeed, in the 3rd edition of "The C++ Programming Language"
Stroustrup refers to strings as "almost containers".  In many ways it is
possible to move through a string in much the same way that you can move
through an STL container.  Treating strings as a type of container means the
string can be composed of "chars, wide chars, unicode chars, whatever."

So, let's cut to the chase.  To quote from an external source, Musser,
Derge, and Saini, "STL Tutorial and Reference Guide", the "STL consists of
six major components:  containers, generic algorithms, iterators, function
objects, adaptors, and allocators."  All of these components can be further
subdivided.

Meyers points out that the best way to appreciate the "STL view of the
world" is to keep in mind the rules for both C++ and C arrays.  "There is
only one rull we need to know: a pointer to an array can legitimately point
to any element of the array or to one element beyond the end of the array."
He illustrates with writing a function to find a particular value in an
array:

int * find(int *begin, int *end, int value)
{
	while (begin != end && *begin != value) ++begin;
	return begin;
}

Ultimately, with the use of templates, and an understanding of the stl, such
a find function can be generalized to whatever datatypes might be contained
within a, well, in a container, written as a template, the find function
above could be rewritten as:

template<class T>
T * find( T * begin, T *end, cont T& value )
{
	while (begin != end && *begin != value) ++begin;
	return begin;
}

Still, while this sort of template permits a greater generalization, one
still needs to know something about the way that begin and end will act.
The STL provides the mechanism to move into all of the container with its
iterators, and each container type supporst different types of iterators.
For example, the list container -- a linked list, really -- cannot support
random access, so the list iterators work by iterating from the first
element until the target element is located, or until the end is reach.  A
vector allows random access iterators, so it is possible to jump into the
middle of a vector and move forwards or backwards from there.  From this,
the programmer knows that you could never do a binary search on a list, but,
if its contents are sorted, you could do a binary search on a vector.
Hence, an STL version of the above find function could be written:

template<class Iterator, class T>
Iterator find(Iterator begin, Iterator end, const T& value)
{
	while ( begin != end && *begin != value ) ++begin;
	return begin;
}

STL, really "is just a collection of class and function templates that
adhere to a set of conventions."  Indeed, reading a reference book like
Josuttis's "The C++ Standard Library" becomes almost tedious, as all
containers include so many similar member functions, begin, end, size, etc,
depending on the sorts of algorithms they support.

Perhaps the most radical idea about the STL is that containers support
certain iterators and iterators enable certain algorithms.  In practice, the
programmer selects the container depending on the operations he or she needs
to perfom on the data it holds, and that should be informed by the most
efficient algorithms to access or manipulate those data.

Finally, the STL is extensible.  While containers can support different data
types (ints, floats, chars, strings, etc.), containers can also hold user
defined data types.  The programmer can add his or her own collection of
iterators and algorithms to the STL family.  "the standard STL collections
will work with your algorithms and your collections will work with the
standard STL algorithms."

Meyers, finally notes that his introduction to the STL does not even begin
to scratch the surface.

Respectfully submitted,

Gregory Haley
Sr. Software Engineer
Venaca, Inc.
New York, NY, USA

_______________________________________________
Effective-cpp mailing list
Effective-cpp at accu.org
http://www.accu.org/mailman/listinfo/effective-cpp



More information about the Effective-cpp mailing list