All pastes #355436 Raw Edit

Computing factorial

public cpp v1 · immutable
·published 2007-02-14 14:13 UTC
rendered paste body
// templates.h#ifndef TEMPLATES_H#define TEMPLATES_Htemplate<int i>class Factorial{public:	enum { val = i * Factorial<i-1>::val };};template<>class Factorial<0>{public:	enum { val = 1 };};#define Fact(X) (Factorial<X>::val)#endif // TEMPLATES_H// templates_main.cpp#include "templates.h"#include <iostream>using namespace std;int main(int argc, char** argv){	int val = Factorial<10>::val;	// mov dword ptr [val],375F00h <---- Single move instruction	cout << "Factorial<10>::val = " << val << endl;	// mov dword ptr [val],375F00h <---- Single move	val = Fact(10);	cout << "Fact(10) = " << val << endl;	return 0;}