C++ templates: the power, the swamp

C++ Source code

Growable array and static table templates

The tar file that can be downloaded from the link below publishes two templates, one for growable arrays and one for building static tables. The documentation for these template based data structures is included in the code as doxygen formatted comments.

Enumeration value to C string (i.e., (const char *)) translation template

Templates: a commentary

Awesome cosmic power!
The Genie, Disney's Aladdin

I have a love/hate relationship with C++. I love the language because it gives me tools to write better software. I hate the language because of its complexity and because the power of C++ can lead the programmer into a swamp created by this complexity.

Two of the most powerful features of C++ are operator overloading and templates. I have written about both the power and the complexity of overload operators elsewhere. I am going to discuss templates briefly here. To illustrate the application of templates, I've included two examples: a template that implements a growable array like the STL vector template and a template to implement static tables.

Templates are powerful because they allow generic code to be written that can be reused for different types. For example, the growable array template can be used to implement growable int, double, or String arrays.

Templates have problems as well. The syntax and semantic errors involving templates can be confusing. A template acts as a kind of macro. Semantic checking is not completely performed until the template is expanded for a given type. When an error is reported the programmer must think about the effect of instantiating the template for a given type and how this might have caused the error. Templates can be even more difficult to debug. A template may be instantiated for several different types, although a runtime error might only occur for one instance.

Each instance of a template reproduces a copy of the template code and data. If templates are heavily used this can cause an explosion in code size. Templates are also a huge challenge for compilers. The C++ language standard allows a template class to be defined in a header file, while the template functions are implemented in a code file (e.g., a *.C or *.cpp file). The causes difficulties when it comes to compiling templates, since the functions in the code file are not complete until the template is instantiated. So the template cannot be fully compiled. Some compilers, like Sun Microsystems's 4.2 C++ compiler do not support template functions that are not in a header file.

The static table template published here shows how convoluted template syntax can be. It took me some time pouring over Stroustrup's The C++ Programming Language, third edition to get the template syntax right. The interaction of templates with features like static class variables is complicated.

And yet...

Templates are powerful. Combined with features like the constructor for global variables, templates allowed me to implement the static table code published here, which would have been difficult to implement without these features.

Ian Kaplan, June 13, 2001
Revised: February 4, 2002


back to Notes on Software and Software Engineering