Subscribe to Code Rants (and Misc. Other Things)        RSS Feed

Template Meta-programming, and You...

Icon Leave Comment
Hello there. If you're reading this, then the word (more of a term really) 'Templated Meta-programming' (TMP) hasn't totally scared the crap out of you, and you're curious as to what this post is about. Right?

OK. First off, let me explain what this is. It's not a TMP tutorial! It's just some opinions on the subject, that may help those newer to the field see it more clearly. I'd also like to state that I'm in no way a 'template guru'. My TMP skills are pretty simple (mainly because I don't practice TMP a lot). What I'm trying to say is this: 'If you already are kinda familiar with the concept, then there's probably not much here for you. It's more of a is it worth considering thing.' :) I also assume all of this in C++, even though TMP can be used in a lot of languages.

Anyways, for those that have never heard of TMP, a good description of it can come from Wikipedia:
Template metaprogramming 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.

So what does this mean? Well, it's often compared to 'a program inside your program that runs when compiled' :/ . To many TMP seems bizarre; mainly because they are of the mind-set that everything happens at runtime, and not compile-time. Many newer C++ kiddies don't realize that things like enums and consts are all figured out as compilation happens. The same with templates :) This means that they can be used during compilation, and given some pretty cool tricks, you can actually solve problems with them.

OK. Like I said this isn't a TMP tutorial, so if you're interested in learning these kinds of things, I suggest you Google it, or look on for a good book.

Now, all this said, I'd like to give a list of things that should always be taken into accountancy when considering TMP.

  • TMP is turing complete, so basically you can do anything useful with it. It's kinda like a C++ inside C++ :D
  • Everything in TMP is compile-time, so if anything is needed at runtime, then it can't be used.
  • It's more type-safe than a macro. Even though macros are easier to read, I'd rather put my confidence with the template system.
  • TMP can increase executable size. I don't recommend loop unrolling for anything bigger than twenty or thirty iterations.
  • TMP get more done at compile-time, so your programs will run faster. (THIS IS THE MAIN MOTIVATION FOR USING TMP)
  • Since TMP is done at compile time, undoubtedly your code will take much longer to compile.
  • TMP programs can be very difficult to read and understand, and you can only rely on compiler errors (since the runtime technically doesn't exits).

All this said, TMP seems to have quite a bit of negativity on it's side. This is true, but I personally see it as being very useful. Basically, in my humble opinion, I'd use it whenever you see a 'non-complex algorithm, or any value that could be evaluated at compile time' (All of this emphasizing simplicity). TMP programs can be a nightmare to work with, so it's also important to keep them organized as well.

Anyways, that's my code rant (even though I wasn't angry). If you've stuck around till the end of all this crap, I thank you. Now get lost! ;)

~ Mike

P.S. There is one other good use for TMP I forgot to mention: 'It can be used to write crazy awesome snippets that'll earn you big kudo here on D.I.C' :P

0 Comments On This Entry


March 2019

1718 19 20212223

Recent Entries

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)