Metaprogramming pt.1

         I was torn between writing a blog on Domain Specific Languages, Simulated Artificial Neural Networks, shell coding in C, and Metaprogramming. Ultimately, I used a random number generator and here we are. To the dismay of my professor, this is a blog about programming, as I will someday port these blogs to my own personal site and this is the area of my interest. Don’t you have any other hobbies besides Computer Science? Honestly, no, I am a quite boring individual. Metaprogramming is a relatively wide encompassing term, and I’ve heard it described as programs that operate on programs (or even themselves), instead of usual data, or code that writes other code. Perhaps you are familiar with Reflections in Java, this is an example of metaprogramming, which I will cover a bit of. I am most familiar with Template Metaprogramming, which is a C++ “discovery”.

         Template Metaprogramming in C++ has an interesting history, so I will cover it in brief here. Erwin Unruh’s prime number computation program was the first concrete example of template metaprogramming, however his program did not finish compiling, rather it output the answer in the compilation error message. This was in 1994 and TMP has come along way since then. [1]
Here is the classical example of TMP:

//Factorial in C++ TMP
template<int N>
struct factorial
{
static const int value = N * factorial<N-1>::value;
};


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


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


int main()
{
//example should be equal to 6!
int example = factorial<6>::value;
return 0;
}

         My experience with TMP is very brief. I read a few hundred pages of C++ Template Metaprogramming by Abrahams and Gurtovoy. It was a fascinating read, but I got carried away with other obligations, and well, TMP is mostly a useful tool in ones toolbox, but certainly not vital to being a good or effective programmer. Another problem is that book is over 10 years old, and the C++ language is always changing. In C++11 we now have “constexpr” which allows simple expressions to be evaluated at compile time (this is essentially what TMP is/does), not only that but C++ now has a lot of standardized TMP types that used to be only available through Boost. Not many books or tutorials have been written on TMP, making it more challenging to learn than other programming idioms. However, I recently stumbled upon Advanced Metaprogramming in Classic C++ by Gennaro, which was published in 2015, and looks promising.

         So why bother with TMP? It can be hard to read, hard to write, and there aren’t as many resources on it. Simple, you want to look cooler than your peers who didn’t bother to learn it. But in reality, it can be a useful tool in many situations, for example, if you want to offload some processing from run-time to compile time, or perhaps you want a fancy macro that minimizes the amount of code you have to write, TMP can come in handy. Or, if you’re like me, you just think programs that run inside the compiler is incredibly neat. Generally, anything that pushes a language to the outer limits, weird and crazy things start to happen, and this is fascinating (albeit very hard to read).

         Not only that, but metaprogramming in general, can be a very powerful tool to generate useful and reusable code, while reducing redundancies. Adding another layer of abstraction (hence the meta in metaprogramming), generally gives large bounds of flexibility and can be a programmers best friend. A lot of tuning programs which create code made specifically for certain architectures are a useful example of metaprogramming, a more specific example that comes to mind standardized Matrix Multiply programs, where other programs find the best way to cache and iterate the matrices and then generate the code for general use. If you’re familiar with the DRY (Don’t Repeat Yourself) principle from The Pragmatic Programmer by Hunt and Thomas, then you’ll see metaprogramming can be a great way to skirt around some potential situations that would otherwise be considered breaking the DRY principle.

         Over the next few weeks, I will be covering all different types of metaprogramming, from C++ TMP, to Java Reflections, to simple Python scripts which generate other code to be compiled. What’s interesting about metaprogramming, is out of the four topics I was deciding to cover, shell coding in C, and Domain Specific Languages, can be considered subsets of metaprogramming to some extent. I will start with a brief introduction to metaprogramming with python, by using python to write working code for another language. Next I will move on to an introduction to TMP in C++ and useful application of it. Afterwards, I will cover Java’s Reflection API, and maybe even Aspect programming with Java (a very useful tool). Finally, I will cover an advanced, but small project that makes use of metaprogramming in two parts.

         Currently I have no idea on what the final project will be. Perhaps some sort of global domain specific language which then compiles into various languages which can be implemented by writing a parser in the respective languages. Maybe it will be a TMP project where I re-implement functions of a standard library in their respective TMP form. Any suggestions are certainly welcome.

 

 

[1] https://en.wikibooks.org/wiki/C%2B%2B_Programming/Templates/Template_Meta-Programming#History_of_TMP

3 Comments

  1. Josie Johnson

    I am very glad you shared this info about the programming languages here. This information is very informative. In case you need help with graphic designs then https://masterbundles.com/ go for this website. I am telling you this with my own experience.

  2. Calvin Wolfe

    I’m focusing on learning HTML and CSS, with the next stage being JavaScript. While learning JavaScript is enjoyable and not overly challenging, I find the extensive written work required to be tedious, especially alongside mastering the fundamental knowledge. Thankfully, I found a resource that offers javascript homework help. This has been a lifesaver for me, especially during busy periods.

Leave a Reply

Your email address will not be published. Required fields are marked *