Template Metaprogramming

Started by
13 comments, last by Paradigm Shift 2000 22 years, 3 months ago
I was reading through my newly purchased Game Programming Gems reference and I found this article about using template metaprogramming to generate constants that are known at compile time. I fail to understand why anyone would need to sacrifice development time, compile time and readability implementing this messy stuff when I could plug the algorithm into a calculator and generate the constant myself. Like for instance, why should I program a metatemplate to generate the sin(1.245) when I could plug the value into my calculator and code it like this:
  
#define CAMERA_ANGLE .9474  // sin(1.245)
  
I suppose in retrospect, a constant generated by a complex polynomial function like the fibonacci sequence would take time to derive by hand, but would it be more time than it took you to program and debug the metatemplate? Under what circumstance would programming the metatemplate be quicker and/or easier, especially since the value must be known at compile time anyway? Paradigm Shift 2000
"I am Locutus of Borg. Resistance is Futile." -- Locutus of Borg
Advertisement
I dont know about you, but I could not find fibonacci(77) in less time then it would take to create and debug a metatemplate. If you keep looking through that article, it goes into matrix operations, and stuff, as well as just the simpler things.

Besides, it''s always fun to just go and say "Ha, Look at what I can do!".

Z.
______________"Evil is Loud"
>>Zaei
Yes it is fun... Unless the response is "Yea, but your game is only 5 fps on my new comp."

-------------
E-)mil
http://eej.dk

- Just another crazy dane
Emil Johansen- SMMOG AI designerhttp://smmog.com
Template Metaprogramming is actually extremely fast.

"You are too useless. And now I must beat you." - English subtitle in a Honk Kong Movie.
=P

Z.
Stupid AP. The above was me.

Z.
______________"Evil is Loud"
quote:
Yes it is fun... Unless the response is "Yea, but your game is only 5 fps on my new comp."

The speed of your program cannot be adversely effected by the use of TMP because the whole point of TMP is to get the compiler to evaluate this stuff for you (ie it is evaluated at compile time, not at run time)

compare the run times of these two programs....

1. The Template Metaprogramming method...

      // fibbonacci.h// class templatetemplate <unsigned int N>class fibbonacci{  public:  static const value = fibbonacci<N-1>::value + fibbonacci<N-2>::value;};// root specializations..class fibbonacci<0>{  public:   static const value = 1;};class fibbonacci<1>{  public:   static const unsigned int value = 1;};#define Fibbonacci(x) fibbonacci<x>::value// main.cpp#include < fibbonacci.h >#include < stdio.h >int main(void){  printf("%ud",Fibbonacci(77));}  


2.The Standard method...

        #include <stdio.h>unsigned int Fibbonacci(unsigned int N){   // base case   if(N == 1 || N == 0)   {     return 1;   }   // else   return (Fibbonacci(N-1) + Fibbonacci(N-2);}int main(void){  printf("%ud",Fibbonacci(77));}      



Edited by - Sandman on September 25, 2001 11:07:42 AM
Er, don''t do fibs that way (the second piece of code), that''s O(N^2), not O(N) which is possible with the fib sol''n. Your main point is correct, however--templated metaprogramming should have zero run-time cost.


I know thats a pretty inefficient way to do it, but I couldnt be arsed to write a sensible one.
Just searched for MP since I''ve just got that bok and am reading it now.

As for the guy who said it''d take him a long time to compute fibbonacci 77:

Fib(n) = ((pow(Phi,n)-pow(-Phi,-n))/sqrt(5);

where Phi=(1+sqrt(5))/2;

Not a lot of people know that (took google long enough to find it for me too!)

- seb

This topic is closed to new replies.

Advertisement