利用模板编译期计算阶乘
来源:互联网 发布:java求二叉树的节点数 编辑:程序博客网 时间:2024/06/12 08:01
//////////////////////////////////////////////////////////////////////////
// C++ templates meta programming
template<bool, typename T1, typename T2>
struct If
{
typedef T1 type;
};
template<typename T1, typename T2>
struct If<false, typename T1, typename T2>
{
typedef T2 type;
};
template<int n>
struct Factorial_exception
{
enum {Value = -1};
};
template<int n>
struct _Factorial
{
enum { Value=n*_Factorial<n-1>::Value};
};
template<>
struct _Factorial<0>
{
enum { Value=1};
};
// 最终结果
template<int n>
struct Factorial
{
enum { Value = If< n<1, Factorial_exception<n>/*小于1时阶乘为-1 */, _Factorial<n> >::type::Value };
};
使用例举:
int i = Factorial<-2>::Value; // 该语句对应编译之后的汇编代码为: mov dword ptr [i],0FFFFFFFFh
amazing?
// C++ templates meta programming
template<bool, typename T1, typename T2>
struct If
{
typedef T1 type;
};
template<typename T1, typename T2>
struct If<false, typename T1, typename T2>
{
typedef T2 type;
};
template<int n>
struct Factorial_exception
{
enum {Value = -1};
};
template<int n>
struct _Factorial
{
enum { Value=n*_Factorial<n-1>::Value};
};
template<>
struct _Factorial<0>
{
enum { Value=1};
};
// 最终结果
template<int n>
struct Factorial
{
enum { Value = If< n<1, Factorial_exception<n>/*小于1时阶乘为-1 */, _Factorial<n> >::type::Value };
};
使用例举:
int i = Factorial<-2>::Value; // 该语句对应编译之后的汇编代码为: mov dword ptr [i],0FFFFFFFFh
amazing?
- 利用模板编译期计算阶乘
- (C++)使用模板在编译期计算阶乘
- TMP(Template metaprogramming)模板元编程的起手程序:编译期计算阶乘
- 模板元编程-编译期计算数值
- 如何使用C++在编译期计算阶乘
- 利用模板偏特化实现编译期断言
- 编译期三角函数计算
- 编译期类型计算示例
- 利用数组计算256的阶乘
- 利用递归计算N的阶乘
- 利用静态变量计算n的阶乘
- 利用静态变量计算n的阶乘
- 利用静态变量计算n的阶乘
- 模板元编程 运行时 编译期
- 在编译期完成数值计算
- 学习笔记:编译期运算,利用constexpr
- 阶乘模板
- 计算阶乘
- 文章统计谈刷新
- java反射机制——解析(2)
- 【技术分享】开Oracle调优鹰眼,深入理解AWR性能报告
- URI和URL的区别
- Linux下c开发 之 线程通信(转)
- 利用模板编译期计算阶乘
- android Spyware Nuhaz.A
- gc buffer busy/gcs log flush sync与log file sync
- 有关递归
- 有n个学生的信息(包括学号、姓名、成绩),要求按照成绩的高低顺序输出各学生的信息。
- _library_cache_advice和latch:shared pool、latch:shared pool simulator
- ELF文件解析和反汇编
- C++数学函数库
- 浅谈程序员的职业规划