Recently, I wrote an Introspection system for C++ with the aid of Randy Gaul’s Blog. There are many ways to achieve this in C++, but the one used by Randy was through Template Metaprogramming. So in order for me to fully understand his implementation, I had to do some research on the subject. And I fell in love with it!

I decided to write a series of posts where I explain what template metaprogramming is, some good reference material to start learning it and then just post some fun programs I’ve made with it.

First, I’d like to start with some Wikipedia quotes on Metaprogramming and Template Metaprogramming:

Metaprogramming is the writing of computer programs with the ability to treat programs as their data. It means that a program could be designed to read, generate, analyse or transform other programs, and even modify itself while running.

Essentially, Metaprogramming is writing code that writes code (hence the Meta part).

Template metaprogramming (TMP) is a metaprogramming technique in which templates are used by a compiler to generate temporary source code, which is merged by the compiler with the rest of the source code and then compiled. The output of these templates include compile-time constants, data structures, and complete functions. The use of templates can be thought of as compile-time execution.

TMP is using templates in order to metaprogram. This was not intended behavior when templates where originally created but they are, by accident, Turing Complete. Darren Grant pointed this out to me through a Facebook comment: “TMP is indeed Turing Complete, but this isn’t very interesting (Lots of things happen to be turing complete, but few are useful). What is more interesting is the way the three rules of untyped lambda calculus can be implemented with TMP, making functional programming possible during the compilation phase”. Therefore, anything that you can do with normal programming, you can do with TMP, it just works a little differently and all operations have to be performed at compile time, so there are no variables.

Now, I’d like to share some resources that have helped me understand the concept of TMP and learn some cool tricks:

Videos

Books

C++ Templates – The Complete Guide – By David VandevoordeThis book is a great one to start with, even if you already know what templates are, since they go in detail about all the intricacies of C++ templates and special cases. It also talks about TMP in Chapter 10.



Modern C++ Design – By Andrei Alexandrescu

Another very good book that uses TMP to solve problems in a “modern” way.



C++ Template Metaprogramming – By David Abrahams

A good resource of intermediate/advanced TMP, once you are a bit more familiar with the concept.

Websites/Blogs

A gentle introduction to Template Metaprogramming with C++ – By Moliate

Template Metaprograms – By Todd Veldhuizen

Finally, I’d like to end this post by sharing my first two TMP programs, which are very simple and found in almost every tutorial about TMP, but I still find them to be very fun.

I’ll keep making posts with TMP tricks as I learn and implement them.

Until next time!