Template Metaprogramming in C++

Standard
Metaprogramming

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


Templates and Metaprogramming – By Randy Gaul


Template Metaprogramming – By John Edwards


Modern Template Metaprogramming: A Compendium, Part I – By Walter E. Brown


Modern Template Metaprogramming: A Compendium, Part II – By Walter E. Brown


Books


C++Templates_CompleteGuide
C++ Templates – The Complete Guide – By David Vandevoorde
This 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.

ModernC++Design
Modern C++ Design – By Andrei Alexandrescu
Another very good book that uses TMP to solve problems in a “modern” way.

cpp_templatemetaprogramming
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


Introduction to Template Metaprogramming – By Nicolas Brailovsky

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.

// Factorial
//------------------------------------------------------------------------------
template <int N>
struct Factorial
{
  static const int value = N * Factorial<N - 1>::value;
};

template <>
struct Factorial<0>
{
  static const int value = 1;
};

int main(void)
{
  std::cout << "Factorial of 6: "  << Factorial<6>::value  << std::endl;
  std::cout << "Factorial of 10: " << Factorial<10>::value << std::endl;

  return 0;
}

// Fibonacci
//------------------------------------------------------------------------------
template <int N>
struct Fibonacci
{
  static const int value = Fibonacci_2<N - 1>::value + Fibonacci_2<N - 2>::value;
};

template <>
struct Fibonacci<1>
{
  static const int value = 1;
};

template <>
struct Fibonacci<0>
{
  static const int value = 1;
};

int main(void)
{
  std::cout << "5th Fibonacci number is: "  << Fibonacci<5>::value  << std::endl;
  std::cout << "20th Fibonacci number is: " << Fibonacci<20>::value << std::endl;

  return 0;
}

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

Until next time!

Advertisement

2 thoughts on “Template Metaprogramming in C++

Share your thoughts

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 )

Facebook photo

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

Connecting to %s