hungarian notation is evil (was Re: Do I always have to write "self." ?)

Michael Hudson mwh21 at cam.ac.uk
Fri May 5 14:36:51 EDT 2000


Alex <cut_me_out at hotmail.com> writes:

> > I like how C++ templates are Turing complete.  That means trying to
> > determine whether or not a C++ program will even compile is equivalent
> > to the Halting Problem.
> 
> How do you do the equivalent of a for loop using templates?

Noisily; it's been a while since I played with this kind of thing, but
hopefuly this will give you some idea:

#include <iostream>

template< int N >
class SumOfSquares {
public:
    enum { val = N*N+SumOfSquares<N-1>::val };
};

template<>
class SumOfSquares<0> {
public:
    enum { val = 0 };
};

int main(void) {
  cout << SumOfSquares<7>::val << endl;
}

then:

[mwh21 at atrus C]$ g++ templatefun.cc
[mwh21 at atrus C]$ ./a.out 
140

[ God, I know C++ too well - I hadn't written any for months before
  that, and I get template meta-programming right basically first
  time... (after I remebered the syntax of enums!) ]

also-known-as-"how-to-crush-your-C++-compiler"-ly y'rs
Michael

-- 
  A student, in hopes of understanding the Lambda-nature, came 
  to Greenblatt.  As they spoke a Multics system hacker walked
  by.  "Is it true", asked the student, "that PL-1 has many of 
  the same data types as Lisp?"  Almost before the student had
  finished his  question, Greenblatt  shouted, "FOO!", and hit 
  the student with a stick.                                -- AI koan



More information about the Python-list mailing list