[prog] Sample implementations of UNIX utilities.

Robert J. Hansen cortana at earthlink.net
Fri Dec 27 16:27:42 EST 2002


> the misleading side. Of *course* the C++ one is going to take bloody *ages* 
> to do, if you're doing weird things with constructors so as to do most of the 
> compilation at compile-time. I call unfair test! :-P

After sending off the last, I realized maybe people didn't understand
what I meant by template metaprogramming--so here's a minor example.

===== Computes factorials in O(1) runtime
===== CUT HERE

#include <iostream>

using namespace std;

template<unsigned long n>
struct factorial
{
  static const unsigned long value = n * factorial<n-1>::value;
};

template <>
struct factorial<0>
{
  static const unsigned long value = 1;
};

int main(void)
{
  cout << "10! = " << factorial<10>::value << endl;
  return 0;
}

===== END CUT





More information about the Programming mailing list