This article is inspired by the C ++ template meta-programming technology and application.
During a C ++ Standards Committee meeting held in San Diego on 1994,
Erwin Unruh shows a special piece of code that can be compiled during the compilation period
The error message is used to generate all prime numbers from 2 to a given value.
"
So I made an implementation in the afternoon. The Code is as follows:
# Include <iostream> using namespace STD;/* declare a prime number checker template ***** I: number to be checked ** factor: testing the I factor ** isprime: whether the previous test passed, that is, whether I is a prime number before the test factor */template <int I, int factor, bool isprime> struct primechecker; Template <int I, int factor> struct primechecker <I, factor, true> {Enum {result = primechecker <I, factor-1, I % factor! = 0 >:: result };}; template <int I, int factor> struct primechecker <I, factor, false >{ Enum {result = false };}; template <int I> struct primechecker <I, 2, true> {Enum {result = (I = 2 | I % 2! = 0) };}; template <int I> struct primechecker <I, 1, true> {Enum {result = true };};/* a fully-specific template. * *** compilation fails after the annotation. In the compilation error output after the annotation, you can find it in the shape **... error: incomplete type 'prime <X, true> 'used in... ** field, which indicates that the number X is the prime number. *** After decoding and commenting, It is compiled and can output the number of prime numbers in a certain range */template <int I, bool B> struct prime {Enum {isprime = true };}; template <int I> struct prime <I, false >{ Enum {isprime = false };}; // used to enumerate prime numbers smaller than I. template <int I> struct enumprime {Enum {primecount = (prime <I, Primechecker <I, I-1, true >:: result >:: isprime? 1:0) + enumprime <I-1 >:: primecount};/* a fully specialized class template ** can actually directly let primecount = 1 **, however, to "print the prime number of errors during compilation "**, or an equivalent prime template */template <> struct enumprime <2> {Enum {primecount = (prime <2, true>: isprime? 1:0) };}; int main () {// used to test whether a certain number is a prime number during compilation. if yes, 1 is output during running; otherwise, 0 cout <primechecker <, true>: Result <Endl; // calculate the number of prime numbers less than a certain number during compilation. cout <enumprime <15 >:: primecount <Endl ;}
Comment out the definition of prime as follows:
/* A fully-specific template. * *** compilation fails after the annotation. In the compilation error output after the annotation, you can find it in the shape **... error: incomplete type 'prime <X, true> 'used in... ** field, which indicates that the number X is the prime number. *** After decoding and commenting, It is compiled and can output the number of prime numbers in a certain range */template <int I, bool B> struct Prime/* {Enum {isprime = true };}*/;
The compilation result is as follows:
The output row marked by the red box in the figure is all the prime numbers within 15.
After commenting on the definition of prime like this, you can compile it and get the following running result:
/* A fully-specific template. * *** compilation fails after the annotation. In the compilation error output after the annotation, you can find it in the shape **... error: incomplete type 'prime <X, true> 'used in... ** field, which indicates that the number X is the prime number. *** After decoding and commenting, It is compiled and can output the number of prime numbers in a certain range */template <int I, bool B> struct prime {Enum {isprime = true };};
The original example mentioned in this article is as follows:
Primzahlen