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?
Compile time factorial
avarionist commented: I'm still working out how exactly it works but man speed glorious speed +1
#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";
}
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.