Thursday, 15 May 2008

Template Metaprogramming: is it worth it?

I split C++ features into 3 classes:
* useless/dangerous: eg, export keyword, throw specifications, uncaught_exception
* significant downsides: eg, template metaprogramming
* essential: eg, const, exceptions, inheritance

The downsides of template metaprogramming are:
(a) inconsistent compiler support across vendors (this is improving all the time though)
(b) a lack of compiler support for decent error messages (being remedied as "concepts" in the next revision of C++, C++0x)
(c) a lack of compiler support for closures (being remedied as lamda functions in C++0x)
(d) a lack of compiler support for external templates ("export" is useless, again this is remedied in C++0x)
(e) a lack of standard library support for metaprogramming, particularly for type traits (again being remedied in C++0x)

I have seen the results of three attempts at different banks to introduce extensive templating into quant libraries using metaprogramming concepts that have failed for the reasons above - these are not theoretical concerns.

In the same way that a gentleman was once defined as "someone who can play the saxophone, but refrains from doing so", I'd regard a decent C++ programmer as someone who's read the literature on this (my own reading includes Josuttis/Vandevoorde. Abrahams/Gurtovoy, Alexandrescu, some of the older papers on expression templates) and then **refrains** from using template metaprogramming unless they have sufficient reason.

So - template metaprogramming? Probably not. Unless it's a good idea. In which case I'm all for it.

No comments: