问题标题: 分享一个有趣的代码

0
0
已解决
薛乘志
薛乘志
初级启示者
初级启示者

输出斐波那契数列第50个数,基于C++的模板元


#include <iostream>

typedef unsigned long long u_ll;
template<u_ll Num> class Fibonacci {
	public:
		const static u_ll Value = Fibonacci < Num - 1 >::Value + Fibonacci < Num - 2 >::Value;
};
template<> class Fibonacci<0> {
	public:
		const static u_ll Value = 1;
};
template<> class Fibonacci<1> {
	public:
		const static u_ll Value = 1;
};

int main() {
	std::cout << Fibonacci<50>::Value;
	return 0;
}

与其他的代码不同,这个代码的斐波那契数是在编译时计算的,就是说实际运行时只执行了输出一个整数的代码,没有计算的过程

(也许CSP可以用这个来水分,因为编译时间是不算运行时间内的)

代码是从网上转载的,觉得有趣分享一下

薛乘志在2022-03-24 21:02:12追加了内容

再转载一个更有趣的代码,如下:

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};
};

template<int i, bool b> struct Prime;
template<int i> struct Prime<i, false> {
	enum {IsPrime = false};
};

template<int i> struct EnumPrime {
	enum {PrimeCount = (Prime < i, PrimeChecker < i, i - 1, true >::Result >::IsPrime ? 1 : 0)
	                   + EnumPrime < i - 1 >::PrimeCount
	     };
};
template<> struct EnumPrime<2> {
	enum {PrimeCount = (Prime<2, true>::IsPrime ? 1 : 0)};
};

int main() {
	EnumPrime<20>::PrimeCount; //在错误信息里打印100以内的质数
	return 0;
}

编译后,devc++提示了一下错误信息(简化+排版后的结果):

	[Error] incomplete type 'Prime<2, true>' used in nested name specifier
In instantiation of 'struct EnumPrime<19>':
	required from 'struct EnumPrime<20>'
	required from here
	[Error] incomplete type 'Prime<19, true>' used in nested name specifier
In instantiation of 'struct EnumPrime<17>':
	recursively required from 'struct EnumPrime<19>'
	required from 'struct EnumPrime<20>'
	required from here
	[Error] incomplete type 'Prime<17, true>' used in nested name specifier
In instantiation of 'struct EnumPrime<13>':
	recursively required from 'struct EnumPrime<19>'
	required from 'struct EnumPrime<20>'
	required from here
	[Error] incomplete type 'Prime<13, true>' used in nested name specifier
In instantiation of 'struct EnumPrime<11>':
	recursively required from 'struct EnumPrime<19>'
	required from 'struct EnumPrime<20>'
	required from here
	[Error] incomplete type 'Prime<11, true>' used in nested name specifier
In instantiation of 'struct EnumPrime<7>':
	recursively required from 'struct EnumPrime<19>'
	required from 'struct EnumPrime<20>'
	required from here
	[Error] incomplete type 'Prime<7, true>' used in nested name specifier
In instantiation of 'struct EnumPrime<5>':
	recursively required from 'struct EnumPrime<19>'
	required from 'struct EnumPrime<20>'
	required from here
	[Error] incomplete type 'Prime<5, true>' used in nested name specifier
In instantiation of 'struct EnumPrime<3>':
	recursively required from 'struct EnumPrime<19>'
	required from 'struct EnumPrime<20>'
	required from here
	[Error] incomplete type 'Prime<3, true>' used in nested name specifier

仔细观察,就会发现一些不得了的东西......


0
已采纳
钱思源
钱思源
高级天翼
高级天翼

别搁那欺负我这个成名猫(给点豆呗~~)

0
万韧山
万韧山
初级天翼
初级天翼

球球你别欺负我这个萌新

 

 

但是豆我要

我要回答