C++ Blog

Template or not to Template

Posted in templates by Umesh Sirsiwal on January 10, 2009

As I was learning C++ over an year ago,  I bought Modern C++ Design by Andrei Alexandrescu. As I read through the book, I was simply amazed by power offered by templates. Experience with the book was followed by Boost libraries. Specifically boost lambda and what it could provide to programmer. These went way beyond traditional STL use of templates. This excitement with C++ templates was followed by my experimentation with using templates in my own projects.  This post summarizes  experiences with templates.

  • Templates provide power with efficiency: Templates provide a lot of power without compromising efficiency. The Andrei’s book is full of examples of such power. A quick look inside the boost library provides examples of such power. Look at simple construct like boost::any which allows you to hold any type of data in it so that you can create a container/array which can hold boolean, integers, your various classes, etc. all at the same time. You can also look at a lot more complex construct like boost::lambda library which brings lambda programming to C++. Recently the boost has added boost::proto discussed in a seperate post on this site. The power really exists. Using these libraries can make your programming life a lot simpler. It is natural tendency to write your own templates on the same line. Should you or shouldn’t you?
  • Templates are not well understood: The templates are not well understood yet. Most experienced C++ programmers find it hard to understand how to program templates. At present templates are a the stage as Object Oriented Programming was several years ago. The compilers have progressed to state where they can correctly compile templates. However, the power of templates is yet to be distilled in to developer community at large. So if you decide to use templates, please be patient and give plenty of time for developers to learn and get up to speed.
  • Template debugging is horrifying experience for first time users: Error messages produced by compiler are horrifying large and complex. For simplest of errors, one gets 3 pages long error message with actual error who knows where. Once you get used to of debugging compilation issues you can generally figure out the error but it takes time to get used to of those errors. Template adaption will go a long way if the compilers start producing better error messages. But if you think compiler was the only issue just wait till you get to debuggers. The debuggers have not yet found a good way to deal with templates. Last but not least is compile time with templates. With most compilers, templates have to be included in header files. A simple change in the template requires compilation of all of your code which uses templates. This is like going backward on libraries.

In my experience, templates offer a lot of power and flexibility without compromising efficiency. The compilers can mostly deal with templates correctly. However debugger, compiler errors, and ways to reduce compile time can go a very long way in helping wider adaptation of templates. But if you are one of those early adapters and technology enthusiast, go ahead experiment and adapt templates and it will go a long way in helping your projects.

If you are going to use templates, you should start off by reading C++ FAQ Lite on template.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: