The 'T' Factor
Where 'T' stands for Templates :). I came across this nifty piece of code on the internet.
Yes, it's supposed to compute the factorial of a number with this statement :-
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. :)
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:
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!
Post a Comment
<< Home