How's your template metaprogramming ninja skills?

Started by
16 comments, last by MJP 11 years, 3 months ago

[quote name='Cornstalks' timestamp='1357885855' post='5020199']

Anyone else have any cool template metaprogramming demos?

[/quote]

Template metaprogramming is cool when it works...

... The problem is when it doesn't compile out.

... Or when someone has to maintain it.

... Or when the code must be supported on multiple compilers.

If the compiler isn't able to discover and take the optimization it will just embed the function calls. These are often deeply-recursive. They also often use algorithms that perform poorly relative to other implementations.

They're great if you're computing a Fibonacci or logarithm of a known constant at compile time. But seriously, how often is that? Even if I did need a logarithm, I'd need to compute it on variables rather than constants.

In code reviews, I am one of many people who routinely reject TMP solutions. Yes they are cool when they work, but they are far too fragile for general case development.

Advertisement
TMP is certainly a tool best applied sparingly. I don't think I've checked in more than a handful of templates in my entire time at my current job, for example.

Even the Epoch compiler is relatively light on template metamagic. I use it in a couple places to decouple things and promote some nifty code reuse tricks, but I try to stay away from it... usage of boost::spirit notwithstanding :-D

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I spent some time on a tmp raytracer a while back: https://github.com/lentinic/TemplateTrace

Fun project for bending the mind a bit.

Pretty cool stuff, people!

As for why my professor was talking about template metaprogramming (and why I'm experimenting with it): some researchers want to write C++ code to run on some super computers, and they're looking into using template metaprogramming to allow them to still write C++ code, but then transform that code at compile time to target the desired super computer (with its millions of CPUs and GPUs). I was just curious of other uses of TMP, even if they're just fun demos and not really useful.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
We had a situation at work about 4 years ago, where a coworker and I wanter to reimplement an old piece of code using signals and slots. I wanted to explicitly plug all the signals to the slots at runtime, but my coworker wanted to do it at compile time using some heavy TMP techniques. Unfortunately our boss agreed to do it the "modern way" (his words, not mine) and we now have a large piece of important code that only a couple of people understand, in a company with dozens of programmers.

My advice: Stay away from TMP. It might be cool, but you should optimize your code for readability, not coolness.
As for why my professor was talking about template metaprogramming (and why I'm experimenting with it): some researchers want to write C++ code to run on some super computers, and they're looking into using template metaprogramming to allow them to still write C++ code, but then transform that code at compile time to target the desired super computer (with its millions of CPUs and GPUs). I was just curious of other uses of TMP, even if they're just fun demos and not really useful.
Interesting, I did a similar thing on the Wii biggrin.png
The Wii doesn't support HLSL for pixel shaders like the 360/etc do, which makes it a real pain to work with. So, I wanted to write my own shader language for it. I didn't really have time to implement a whole language, but I was on a TMP-kick at the time, so I instead hijacked C++ as my shader language! I wrote a TMP library which converted HLSL-looking C++ code into the necessary structures to perform pixel operations on the Wii's GPU.
e.g.
PixelShader shader;
{
 TexCoord<0> uv;
 Texture<0> diffuseMap;
 Register<0> output;
 Uniform<0> multiplier;
 
 output = diffuseMap(uv);
 output.rgb *= multiplier.rgb;
} return shader;
The same problem that others have expressed applies though -- I was the only one that understood the TMP code, and now, years later, the workings of it are even foreign to me.
The only useful/practical use I have ever had for this type of template metaprogramming was to calculate the size of a quad-tree at compile-time depending on how many levels I want there to be.

For 8 levels you would have 21,845 cells and it is best to keep them in 1 contiguous memory block made via 1 allocation. Not only does this improve cache performance but it allows for instant insertion into any cell in the tree. I plan to explain this method for creating a quad-tree in an upcoming tutorial as it is extremely efficient.

I don’t need to post code for such a simple thing but this is an example of a real-world situation where these tricks that allow compile-time Fibonacci sequences can be usefully employed.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

The only thing I've written that made really heavy use of template shenanigans was for a home project where I wrote a really flexible mesh baking system for baking out various forms of lighting and AO so that I could test them out. I would never check in anything like that at work, since it's a nightmare for other people to understand and maintain. I have no problem with typing out a little more code if it's easier to understand and/or less bug-prone.

This topic is closed to new replies.

Advertisement