Sunday, August 05, 2007

The 'T' Factor

Where 'T' stands for Templates :). I came across this nifty piece of code on the internet.


template<int N>
class Factorial {
public:
enum { value = N * Factorial<N-1>::value };
};

class Factorial<1> {
public:
enum { value = 1 };
};

Yes, it's supposed to compute the factorial of a number with this statement :-

Factorial<N>::value

And all that happens, as with templates, at compile time!.Read this for further explanation.
Wow, so no worrying about integer overflows while computing factorial anymore. As the program won't compile in that case. The possibilities are endless. :)

1 Comments:

Blogger Swati said...

Nice to see a technical post after a long time..

Interesting snippet. Once you get through the barrier of 'intimidating syntax/understanding' of templates, the possibilities are endless!

Yes, I am yet to get over the barrier ;-)
I am all set to read the article. Thanks!

Monday, August 06, 2007 8:59:00 AM  

Post a Comment

<< Home