这个程序不能通信编译,说明它并不是运行在运行期,它借助编译错误信息报出'指定范围的素数',比如下面程序将会在primes<x>中x是素数的类中发生错误,看错误信息吧...
#include <cstdlib>
#include <iostream>
using namespace std;
struct true_type
{
private:
true_type(){};
};
struct false_type{};
template<bool,typename,typename B>
struct IfThenElse
{
typedef B result_type;
};
template<typename A,typename B>
struct IfThenElse<true,A,B>
{
typedef A result_type;
};
template<int d,int i = d/2 >
struct is_prime
{
typedef typename IfThenElse<d%i==0,false_type,typename is_prime<d,i-1>::result_type>::result_type result_type;
};
template<int d>
struct is_prime<d,1>
{
typedef true_type result_type;
};
template<int d>
struct primes
{
typedef typename is_prime<d>::result_type result_type;
static void show()
{
result_type y;
primes<d-1>::show();
}
};
template<>
struct primes<1>
{
static void show(){}
};
int main(int argc, char *argv[])
{
primes<10>::show();
system("PAUSE");
return EXIT_SUCCESS;
}
g++.exe -c main.cpp -o main.o -I"D:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"D:/Dev-Cpp/include/c++/3.4.2/backward" -I"D:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"D:/Dev-Cpp/include/c++/3.4.2" -I"D:/Dev-Cpp/include"
main.cpp: In static member function `static void primes<d>::show() [with int d = 7]':
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 8]'
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 9]'
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 10]'
main.cpp:59: instantiated from here
main.cpp:9: error: `true_type::true_type()' is private
main.cpp:46: error: within this context
main.cpp: In static member function `static void primes<d>::show() [with int d = 5]':
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 6]'
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 7]'
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 8]'
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 9]'
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 10]'
main.cpp:59: instantiated from here
main.cpp:9: error: `true_type::true_type()' is private
main.cpp:46: error: within this context
main.cpp: In static member function `static void primes<d>::show() [with int d = 3]':
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 4]'
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 5]'
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 6]'
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 7]'
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 8]'
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 9]'
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 10]'
main.cpp:59: instantiated from here
main.cpp:9: error: `true_type::true_type()' is private
main.cpp:46: error: within this context
main.cpp: In static member function `static void primes<d>::show() [with int d = 2]':
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 3]'
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 4]'
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 5]'
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 6]'
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 7]'
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 8]'
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 9]'
main.cpp:47: instantiated from `static void primes<d>::show() [with int d = 10]'
main.cpp:59: instantiated from here
main.cpp:9: error: `true_type::true_type()' is private
main.cpp:46: error: within this context
make.exe: *** [main.o] Error 1
执行结束
1994年Erwin Unruh提供了一个类似的程序。这个程序没什么用,甚至不能运行,但是我觉得很酷,因为它是编译期‘工作’的,我的电脑配置低貌似会很慢,编译起来比较慢。
评论