2

This is a simple demonstration of meta programming. It shows the idea
of meta programming. It basically calculates the factorial of a number
before runtime. When I first saw the concept of meta programming,
it blew my mind so badly. I was so surprised that one could do this.
Its soooo kewl, don't you think?

Edited by firstPerson: n/a

Votes + Comments
I'm still working out how exactly it works but man speed glorious speed
#include <iostream>

using std::cout;

template<int val>
struct staticFactorial{ 
	enum {result = val*staticFactorial<val-1>::result}; 
};
//template specilization
template<> struct staticFactorial<0>{ 
	enum {result = 1};
};

int main(){
	int A[] = {         staticFactorial<0>::result,
			    staticFactorial<1>::result,
			    staticFactorial<2>::result,
			    staticFactorial<3>::result,
			    staticFactorial<4>::result,
			    staticFactorial<5>::result,
			    staticFactorial<6>::result,
			    staticFactorial<7>::result
		     };

	for(int i = 0; i < sizeof(A)/sizeof(A[0]); ++i){
		cout << A[i] << "\n";
	}

}
3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by griswolf
0

This is a simple demonstration of meta programming. It shows the idea
of meta programming. It basically calculates the factorial of a number
before runtime. When I first saw the concept of meta programming,
it blew my mind so badly. I was so surprised that one could do this.
Its soooo kewl, don't you think?

that was wicked fast nice bit :P

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.