/
githubmirror
/
CPlusPlusThings
Обзор
Документация
Войти
/
githubmirror
/
CPlusPlusThings
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
learn_class/modern_cpp_30/compilercompute/WhileLoop.cpp
57 строк
1 KB
zhangxing
support bazel complie this project and format code.
30 мар 2024, 17:00
30 мар 2024, 17:00
3c8a3f2
Код
Авторство
О чём код?
// // Created by light on 20-1-5. // #include <iostream> using namespace std; // 使用template实现while循环 template <bool condition, typename Body> struct WhileLoop; template <typename Body> struct WhileLoop<true, Body> { typedef typename WhileLoop<Body::cond_value, typename Body::next_type>::type type; }; template <typename Body> struct WhileLoop<false, Body> { typedef typename Body::res_type type; }; template <typename Body> struct While { typedef typename WhileLoop<Body::cond_value, Body>::type type; }; template <typename Body> using While_t = WhileLoop<Body::cond_value, Body>; namespace my { template <class T, T v> struct integral_constant { static const T value = v; typedef T value_type; typedef integral_constant<T, v> type; }; } // namespace my template <int result, int n> struct SumLoop { // 循环的条件 static const bool cond_value = n != 0; // 循环后的结果 static const int res_value = result; // 循环时的状态 typedef my::integral_constant<int, res_value> res_type; // 循环执行一次时的状态 typedef SumLoop<result + n, n - 1> next_type; }; template <int n> struct Sum { typedef SumLoop<0, n> type; }; template <int n> using Sum_t = SumLoop<0, n>; int main() { cout << While<Sum<6>::type>::type::value << endl; cout << While_t<Sum_t<6>>::type::value << endl; return 0; }