- 需求
- 思路
- 源代码
- 结果展示
- 总结
考虑两个 10 进制非负整数的相加,如:199 + 1 = 200,35635368953573875834674647 + 3579847859469476976 = 35635372533421735304151623 。
由于该整数的长度可能超过c++基本类型的范围,因此需要通过数组的方式来存储,并计算相加的结果。
要求不运行代码即可得出结果。
思路- 定义模板类Cont,表示数组,整数的每一位都是数组元素。
- 定义打印类print,其成员函数fun()可以依次打印数组的值。
- 定义翻转数组的类Reverse,可实现整数的翻转,即:低位在前,高位在后。
- 实现加法逻辑,考虑进位和不同长度的整数相加。
- 调用之前实现的数组翻转元函数,将结果数组翻转,确保高位位于数组的开头,低位位于数组的结尾。
- 调用之前实现的 print 函数,打印输出结果。
#include结果展示using namespace std; // 定义数组 template struct Cont; // 打印编译器的数组 template struct print; template struct print > { inline static void fun() { cout << T1; print >::fun(); } }; template <> struct print > { inline static void fun() { cout << endl; } }; // 翻转数组 template struct Reverse; template struct Reverse , Cont > { using type = typename Reverse , Cont >::type; }; template struct Reverse , Cont<>> { using type = Cont ; }; // 数组按位相加 template struct Plus; template struct PlusExt; template struct PlusExt > { constexpr static unsigned give = 0; constexpr static unsigned tmp = T; }; template struct PlusExt = 10)>> { constexpr static unsigned give = 1; constexpr static unsigned tmp = T - 10; }; template struct Plus , Cont , Cont , Cont > { constexpr static unsigned tmp = PlusExt ::tmp; constexpr static unsigned give = PlusExt ::give; using type = typename Plus , Cont , Cont , Cont >::type; }; // t1数组的长度大于t2数组 template struct Plus , Cont , Cont , Cont<>> { constexpr static unsigned tmp = PlusExt ::tmp; constexpr static unsigned give = PlusExt ::give; using type = typename Plus , Cont , Cont , Cont<>>::type; }; // t1数组的长度小于t2数组 template struct Plus , Cont , Cont<>, Cont > { constexpr static unsigned tmp = PlusExt ::tmp; constexpr static unsigned give = PlusExt ::give; using type = typename Plus , Cont , Cont<>, Cont >::type; }; template struct LastResult; template struct Plus , Cont , Cont<>, Cont<>> { using type = typename LastResult , Cont >::result; }; template struct LastResult , Cont > { using result = std::conditional_t , Cont >; }; int main() { using a1 = Cont<1, 9, 9>; using a2 = Cont<1>; using a1R = Reverse , a1>::type; using a2R = Reverse , a2>::type; using a1Rplusa2R = Plus , Cont<0>, a1R, a2R>::type; using a1Plusa2 = Reverse , a1Rplusa2R>::type; print ::fun(); return 0; }
代码写好后软件自动提示结果(IDE为vscode),编译前就可显示出来。
元编程是一种强大的编程技术,能在编译期就完成部分软件功能,从而达到提高软件执行效率、验证代码逻辑和数据规范等功能。
但元编程的编码方式比较特殊,需要花时间练习。
以上元编程代码中用到了以下技巧:
- 类模板
- 变长模板
- 类模板的特化和限制
- 元编程的分支和循环语句



