• Advertisement

Archived

This topic is now archived and is closed to further replies.

Template Metaprogramming

This topic is 5877 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
>>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

Share this post


Link to post
Share on other sites
Template Metaprogramming is actually extremely fast.

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
=P

Z.

Share this post


Link to post
Share on other sites
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 template

template <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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites


I know thats a pretty inefficient way to do it, but I couldnt be arsed to write a sensible one.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
well it''s actually a deeper question than time of doing things ONCE ... it''s also balancing the risk of having to do it again, verses the expected tradeoff. The poster who used #define camera_angle is already goin halfway there - here''s why:

You are #defining a constant at the top of a file, to allow it to be changed in once place later, instead of many. And if you code very well, you probably get #defined consts in files that are defined using other # defined consts ... like this:

#define KEYS_PER_SET 8
#define SETS_PER_CONSOLE 7
#define NUM_KEYS (KEYS_PER_SET * SETS_PER_CONSOLE)

...

well .. template meta programming is THE SAME THING ... it is putting the steps you use on the calculator, INTO CODE, so they take no repeated effort in the future, that is prone to being ignored / forgotten / or messed up.

How would you use a calculator to change the 1.245 in ONE STEP ... if you had THREE constants based on the original 1.245 number ... such as camera angle, camera angle normal, and camera offset. Instead of computing 3 numbers each time with three short mathmatical burst on your calculator ... why not spend 3-5 minutes, and express those math operations in C code ...

of course it''s a constant balance ... you must always decide based on YOUR talents / interests / and comfort level, when to express a solution in code, and when to solve it by hand.

Share this post


Link to post
Share on other sites
quote:
Original post by BSXrider
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


Umm... there is a much shorter, and easier, way to do that:


Fib(n) = ((n*n)+n)/2;


for all n > 0



-Steven

Share this post


Link to post
Share on other sites
quote:
Original post by BSXrider
Er... "no"

n=4. 1/2((n*n)+n) = 10.

fib(4) = 3

- seb


Sorry, I was thinking of summation. Been too long since I have had a math class.

-Steven

Share this post


Link to post
Share on other sites
Personaly i''m lazy myself. I prefer the define Method over the Template method. And the problem with the calculations is no problem if you just use maple7 or mathematica for the constants you need in your code. maple7 just uses one line for fib(77) or any other value. So no need to type it in a calculator or know the correct formula for such things.

Share this post


Link to post
Share on other sites

  • Advertisement