C++ Meta programming madness?

| 6 Comments | 1 TrackBack

I find myself agreeing with Chris on this one. I just dont think I'm clever enough to use these 'modern' C++ template programming styles reliably in a way that makes the resulting code better and easier to maintain by others. I've tried, but I tend to end up with a confusing mess that I can't understand when I come back to it. What's more I often find that I can't justify the additional complexity of the result compared to doing the work in the 'old-school' non template way.

I also think that there may be some truth in Chris's comment that; Template programming has become a mental game for those who want to be C++ giants. It is a proving ground. C++ templates are not a meta-programming language. Those that use templates for advanced meta programming are abusing a side effect of a language construct. The fact that the syntax is so obtuse (because it was never intended to be used in this way) provides a way to prove mental prowess.

I know one thing. I'm certainly not smart enough to work out how best to use this stuff; I'll wait for a definitive reference to come along in a few years time and, for the time being, stick to my rule that I don't want to see that kind of stuff in production systems...

1 TrackBack

Starting from Raymond Chen's talk about optimization often beeing counter-intuitive and Christopher Baus' thoughs about low-level C++ optimization in general, I give my 2 cents about if low-level optimization really matters. Read More

6 Comments

The definitive book will likely be David Abrahams book which is in the works. The second is Modern C++ Design by Alexandrescu. Between the 2 you get Policy Based Design and lower level meta programming. I make some broad generalizations in this entry (throwing template meta programming and PDB in one heap), but hell that's what blogs are all about.

I am breaking with the core C++ community. I have some serious reservations with the template implementation in C++. Templates do not work for interface specification with out concept checking. This needs to get into C++, but I just don't see it happening any time too soon.

Plus the arguments of performance gains are bogus. Modern CPUs can handle the indirection of dynamic dispatch virtually instantly. Plus with dynamic dispatch the same code can get executed from multiple places in the code. Inlining makes code execution slower as the code gets bigger.

I'll look out for the Abrahams book. I have Modern C++ Design but I don't think it does enough to show when the meta programming technique adds value. It's all very clever but I'd like something along the lines of the Sutter Exceptional C++ series that shows where and when template meta programming is a win and what the trade-offs and drawbacks are.

Policy based design is a useful technique but it doesn't necessarily require templates to make it work in all cases...

As I've said before, I like templates but I don't think they're actually necessary most of the time.

i have to disagree with these sentiments. templates are tools, just like inheritance. its a feature of the language...don't want to use it, then don't. no reason to nagate their usefulness, "most of the time".

"As I've said before, I like templates but I don't think they're actually necessary most of the time." - so why *do u* like templates then? do you think that STL is a bunch of overengineered stuff?

i reckon that meta-programming in particular is a very useful concept, one that more developers should be catching on to: if u can delegate more work away from your plate and onto the compiler's plate - i think it can only be a win.

sure, the syntax might be hairy, learning curve steep, but that's life. i remember being puzzled by the apparent complexity of C++ vs C (back in the day). all this went away after understanding the concepts a bit better. but, it can still be argued that all that you can achieve in C++ can be done in C (the first C++ 'compilers' where written in C afterall).

i see meta-programming concepts as a step forward - may it live to see the dawn ;-)

I agree that templates are just another tool in the tool box and that sometimes they're appropriate and sometimes they're not.

I like templates for providing generic code where it's necessary. It's not always necessary. The STL works well, but much application code needn't be quite that flexible. Most of us aren't producing code that has to be quite as generic as the STL is. When code does need to be that generic then templates are often a good fit, but there are often other techniques that may be as appropriate. The complicated part is deciding when it's good to use a tool and when other tools are more appropriate and these decisions always depend on other design decisions so I find it usually comes down to a judgement call...

I currently tend to favour minimising the creation of templates, usually only using them where type safety is important and where other techniques can't give me the same result. Even then I personally prefer to keep the template code as thin as possible, often as a type-safe shim over a more old-school generic implementation - if only due to a possibly misguided desire to keep the compile times down...

Template meta-programming is interesting and has potential but, as I said in my original post, I'm simply not clever enough to decide when it actually adds value over more traditional methods. So I'm waiting for a book to tell me; and no, I don't think that the Alexandrescu book is the book - it introduces the technique, I want a book that shows how using the meta-programming techniques compares with how it could be done without; compare and contrast and show me how it's better. Of course if anyone fancies writing something that does this compare and contrast job then I'd love to read it.

Steep learning curves and bleeding edge techniques are great if it's factored in to your schedules, if all of the team members are up to it and if management accepts the risk; if not then I'd prefer to keep such fun stuff for small learning projects so that the risk can be managed and stick to tried and tested techniques for production systems with tight deadlines. KISS - so until more developers are fully conversant with the technique I think it should be viewed with some caution lest you end up with code that's harder to maintain than it need be.

Meta template programming can be a tool too. I see it as offering a wider range of control over low-level optimization.
Perhaps it should stay within the realm of ones personal code and not enter into commercial projects, but that is for the project managers and programmers to decide.

The usefulness of this technique is up to the programmers who use them. The following is the URL of a slightly interesting article with a few obvious uses;

http://osl.iu.edu/~tveldhui/papers/Template-Metaprograms/meta-art.html

Johan

Thanks for the link; it's an interesting article. I agree with you, the usefulness of the tool depends on the user. In a team where everyone is fluent in its use then template meta programming is a great addition to the tool box. The trick is finding teams like that :)

Leave a comment