I just found out what metaprogramming is today, and my head is going to explode. Talking about using template class to solve recursion. This is pretty Jedi stuff. How many of you actually doing this on a regular basis?
Printable View
I just found out what metaprogramming is today, and my head is going to explode. Talking about using template class to solve recursion. This is pretty Jedi stuff. How many of you actually doing this on a regular basis?
It's not precisely recursion, it's more akin to conditional iteration.
I've yet to encounter anyone who regularly writes new "metaprogramming" code in a production environment - the code written using these techniques will tend to be reused on a regular basis, rather than rewritten from scratch - but it is certainly nice to have when needed.
Yeah I've made a few that I re-use. The most used one is one that calculates the string length of an integer constant. In other words it does 1 + ceil(log base 10).
One does not tend to write new ones very often, but it's fun writing them initially.
Depends what exactly "metaprogramming" is. If you mean using templates to generate code and/or data types at compile time, then this is a widely used technique.
Most of the sites that give introductions to this topic tend to use really silly or unrealistic examples which leave you with the impression that this is a toy that nobody really uses. Not the case.
The Boost library includes some very impressive examples of metaprogramming that are actually extremely useful. I think they go way off the deep end with some of it, but it's a good place to start looking at real examples of how it's done.
thanks for the responds, guys.
What do you mean? I just saw this website this guy made the template class and used it just like recursive call to calculate factorial.Quote:
It's not precisely recursion, it's more akin to conditional iteration.
Maybe, I'm missing something here.Code:template< int i >
class FACTOR{
public:
enum {RESULT = i * FACTOR<I-1>::RESULT};
};
class FACTOR< 1 >{
public:
enum {RESULT = 1};
};
yeah, it seems like a very good practice and fun to do until I'd hit some bug and start banging my head on the table :-)Quote:
One does not tend to write new ones very often, but it's fun writing them initially.
Those guys working on Boost are very cutting edge on C++. I'm reading the the paper from the mpl and hope that I won't get lost.Quote:
The Boost library includes some very impressive examples of metaprogramming that are actually extremely useful.
I have talked to a university professor who does lots of C++ code reviews and he encounters a lot of template code that even he after 15+ years of C++ experience can't decipher at once and still needs a book
Yeah, I do some meta-template programming from time to times. Lots of recursion stuff. Sometimes types.