// prime.C
// ----------------------------------------------------------------
// COMP 290-001: Algorithm Library Design, Lutz Kettner, 01/11/2000
// Program by Erwin Unruh, adapted to C++ standard.
// Compile with: g++-2.95.1 -ftemplate-depth-50 -c prime.C | & grep conversion
// or on SGI's:  CC -LANG:std -c prime.C | & grep unnamed

// Class to create "output" at compile time (error messages)
template <int i, int prim> struct D {};
template <int i> struct D<i,0> { D(int);};

// Class to compute prime condition
template <int p, int i> struct is_prime {
    enum { prim = ((p%i) && is_prime< (i>2 ? p : 0), i-1>::prim) };
};
template<> struct is_prime<0,1> { enum { prim = 1}; };
template<> struct is_prime<0,0> { enum { prim = 1}; };

// Class to iterate through all values: 2..i
template <int i> struct Prime_print {
    Prime_print<i-1> a;
    enum { prim = is_prime<i,i-1>::prim };
    void f() { a.f(); D<i,prim> d = prim; }
};
template<> struct Prime_print<2> { 
    enum { prim = 1};
    void f() { D<2,prim> d = prim; }
};

void foo() {
    Prime_print<50> a;
    a.f();
}


