Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |
Templates go far beyond simple type parameterization. When
you combine argument type deduction, custom specialization, and template
metaprogramming, C++ templates emerge as a powerful code generation mechanism.
One of the weaknesses of C++ templates that we did not
mention is the difficulty in interpreting compile-time error messages. The quantity
of inscrutable text spewed out by the compiler can be quite overwhelming. C++
compilers have improved their template error messages, and Leor Zolman has written a tool called STLFilt that renders these error messages much
more readable by extracting the useful information and throwing away the rest.
Another important idea to take away from this chapter is
that a template implies an interface. That is, even though the template
keyword says I ll take any type, the code in a template definition requires
that certain operators and member functions be supported that s the interface.
So in reality, a template definition is saying, I ll take any type that
supports this interface. Things would be much nicer if the compiler could
simply say, Hey, this type that you re trying to instantiate the template with
doesn t support that interface can t do it. Using templates constitutes a sort
of latent type checking that is more flexible than the pure object-oriented
practice of requiring all types to derive from certain base classes.
In Chapters 6 and 7 we explore in depth the most famous
application of templates, the subset of the Standard C++ library commonly known
as the Standard Template Library (STL). Chapters 9 and 10 also use template
techniques not found in this chapter.
Thinking in C++ Vol 2 - Practical Programming |
Prev |
Home |
Next |