[ create a new paste ] login | about

Link: http://codepad.org/MJzELxpQ    [ raw code | output | fork ]

nilukush - C++, pasted on May 22:
#include <iostream>

template <int p, int i>
class is_prime
{    public:
     enum { prim = (p==2) || (p%i) && is_prime<(i>2?p:0),i-1>::prim }; 
};

template<>
class is_prime<0,0>
{    public:
     enum {prim=1};
}; 

template<>
class is_prime<0,1>
{    public:
     enum {prim=1};
};

template <int i>
class D
{    public:
     D(void*);
};

template <int i>
class Prime_print
{    // primary template for loop to print prime numbers
     public:
     Prime_print<i-1> a;
     enum { prim = is_prime<i,i-1>::prim };
     void f()
     {    D<i> d = prim ? 1 : 0;
          a.f();
     }
};

template<>
class Prime_print<1>
{    // full specialization to end the loop
     public:
     enum {prim=0};
     void f()
     { D<1> d = prim ? 1 : 0; };
};

#ifndef LAST
#define LAST 18
#endif

int main()
{   Prime_print<LAST> a;
    a.f();
    return 0;
}


Output:
1
2
3
4
5
t.cpp: In member function 'void Prime_print<i>::f() [with int i = 17]':
t.cpp:36:   instantiated from 'void Prime_print<i>::f() [with int i = 18]'
t.cpp:55:   instantiated from here
Line 34: error: conversion from 'int' to non-scalar type 'D<17>' requested
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: