Jump to content
  • Advertisement

Archived

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

Paradigm Shift 2000

Template Metaprogramming

This topic is 6109 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
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
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!