Mohammad said:
"Jonathan Turkanis" <
[email protected]> wrote in message
Thanks Jonathan!
What does the metaprogramming stand for? Can someone explain briefly.
Metaprogramming is not something that can be explained briefly. Search
the web for it.
"Meta" means "in the vicinity of". If programming is making a computer
behave in a certain way based on input data, metaprogramming is making
compiler do something based on source code (as its input data). Well,
not limited to that of course...
Compile-time calculation of certain things can be given as an example
of metaprogramming. For instance, instead of making computer calculate,
say, factorials of some numbers (which can be needed often) and instead
of precalculating them yourself and putting in a table, you could write
a metaprogram using templates to calculate it:
template<unsigned i> struct factorial {
enum { value = i * factorial<i-1>::value }; // recursive
};
// to stop the recursion from becoming infinite
template<> struct factorial<0> { enum { value = 1 }; };
template<> struct factorial<1> { enum { value = 1 }; };
int main() {
int fact10 = factorial<10>::value; // compiler will calculate
return 0;
}
In the example above 'value' enumerator is calculated based on recursive
instantiation of templates required and utilisation of given template
specialisations for i==1 and i==0 (just in case somebody needs it). The
compiler has to precalculate the value initialisers by instantiating all
the intermediate templates but it won't have to do that during run-time,
and there is no enormous table to type (and make a mistake in).
Victor