Wednesday, April 27, 2005

C++ Template Metaprogramming

I've just finished reading C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond, and I'll admit it's blown my mind. I had no idea that the template resolution system in C++ could be so powerful. C++ is a huge language, but this book makes me realise that it's a lot bigger than I expected.

In my first year in University we were taught Miranda, a functional programming language. At the time we wondered why we were being taught a relatively unknown language with few real practical applications. The functional aspects of Miranda made it very different to the the procedural languages that most of us had encountered previously (which was of course one of the reasons why they taught it to us). However, I'm very glad that we were taught Miranda all those years ago, as it's made understanding the concepts of other functional langauges such as XSLT and now the Boost MPL much easier.

The book is well presented and readable, and it provides a great overview of what template metaprogramming is all about and how the Boost MPL can help you. The middle sections of the book go into the details of how and why the MPL works; if you find this heavy going then I suggest you read the first few chapters and the last few, as these concentrate on the uses of template metaprogramming in C++.

I particularly liked the concept of Domain Specific Embedded Languages (DSEL) which are outlined in the last few chapters. In essence these are micro-programming languages for specific problem domains that are embedded in C++. As an example, the last chapter shows how to implementate a language to generate finite state machines using the MPL. The implementation ends up being beautifully efficient as most of the work is done at compile time.

This book is well worth reading, but you'll need a good understanding of templates to do so.


Post a Comment

<< Home