| |
 | Template Specializations
By Danny Kalev, C++ Pro
|
The generic nature of templates enables you to define molds of classes and functions from which compilers generate concrete instances, or specializations. For example, the primary template std::vector may produce the following specializations:
vector <int> vi;
vector <string> vs;
vector <Date> vd;
//...and so on.
Likewise, function templates such as random_shuffle() can generate a sequence of integers, string, files etc. The type-unawareness of templates is what makes them so useful. Yet there are times when type-unawareness becomes a problem.

While the benefits of generic algorithms and classes are obvious, certain programming tasks still need to be type-aware in order to function properly. How can you achieve this?

Use template specializations and partial specializations to override the default behavior of a primary template in special cases.
|
|
Find Out More
DevX 10-Minute Solution: Using the random_shuffle() Algorithm to Randomize a Sequence of Elements
DevX 10-Minute Solution: Optimize Abstract Operations with Function Templates
DevX 10-Minute Solution: Defining a Function Object
DevX TechTip: Template Specialization
Standard Template Library Programmer's Guide
All DevX 10-Minute Solutions for C/C++
 | TALK BACK |
In your opinion, what is the most difficult aspect of using templates? Cryptic compilation errors? Cumbersome syntax? Anything else?
 |
|