C++编译期计算的探索
348 字
2 分钟
C++编译期计算的探索
先放个代码在这 解释下次再说吧(摸了
1#include <iostream>2#include <utility>3#include <array>4
5namespace compiling {6#define DEF_BINARY_OP(NAME, OP)\7 template <auto N1, auto N2>\8 struct NAME##_t : std::bool_constant<N1 == N2> {};\9 template <auto N1, auto N2>\10 constexpr auto NAME##_v = NAME##_t<N1, N2>::value;11 DEF_BINARY_OP(less_than, <)12 DEF_BINARY_OP(less_than_or_equals, <=)13 DEF_BINARY_OP(greater_than, >)14 DEF_BINARY_OP(greater_than_or_equals, >=)15 DEF_BINARY_OP(equals, ==)16 DEF_BINARY_OP(not_equals, !=)17
18 template<typename value_type, int N>19 struct factorial_t : std::integral_constant<value_type, N * factorial_t<value_type, N - 1>::value> {};20#define SPECIALIZE_FACTORIAL_T(N, V) template <typename value_type> struct factorial_t<value_type, N> : std::integral_constant<value_type, V> {};21 SPECIALIZE_FACTORIAL_T(0, 0)22 SPECIALIZE_FACTORIAL_T(1, 1)23 template<typename value_type, int N>24 constexpr auto factorial_v = factorial_t<value_type, N>::value;25 template<typename value_type, auto ...N>26 constexpr auto _gen_factorial(std::index_sequence<N...>) {27 return std::array<value_type, sizeof...(N)>{factorial_t<value_type, N>::value...};28 }29 template<typename value_type, int N>30 constexpr auto gen_factorial() {31 return _gen_factorial<value_type>(std::make_index_sequence<N>{});32 }33
34 template<typename value_type, int N>35 struct fib_t : std::integral_constant<value_type, fib_t<value_type, N - 1>::value + fib_t<value_type, N - 2>::value> {};36#define SPECIALIZE_FIB_T(N, V) template<typename value_type> struct fib_t<value_type, N> : std::integral_constant<value_type, V> {};37 SPECIALIZE_FIB_T(0, 0)38 SPECIALIZE_FIB_T(1, 1)39 SPECIALIZE_FIB_T(2, 1)40 template<typename value_type, int N>41 constexpr auto fib_v = fib_t<value_type, N>::value;42
43 template<typename value_type, auto ...N>44 constexpr auto _gen_fib(std::index_sequence<N...>) {45 return std::array<value_type, sizeof...(N)>{fib_t<value_type, N>::value...};46 }47
48 template<typename value_type, int N>49 constexpr auto gen_fib() {50 return _gen_fib<value_type>(std::make_index_sequence<N>{});51 }52
53 template<int N, int INDEX>54 struct is_prime_t {55 // byd不能短路求值是吧56 // constexpr static bool value = INDEX * INDEX > N || N % INDEX != 0 && is_prime_t<N, INDEX + 1>::value;57 constexpr static bool get() {58 if constexpr (INDEX * INDEX > N) {59 return true;60 } else {61 return N % INDEX != 0 && is_prime_t<N, INDEX + 1>::get();62 }63 }64 };65
66 template<int INDEX>67 struct is_prime_t<2, INDEX> {68 constexpr static bool get() {69 return true;70 }71 };72
73 template<int N>74 constexpr bool is_prime_v = is_prime_t<N, 2>::get();75
76 template<auto ...N>77 constexpr auto _gen_prime(std::index_sequence<N...>) {78 return std::array<bool, sizeof...(N)>{is_prime_t<N, 2>::get()...};79 }80
81 template<int N>82 constexpr auto gen_prime() {83 return _gen_prime(std::make_index_sequence<N>{});84 }85}86void compiling_test() {87 // ==========static test==========88 // 19! = 12164510040883200089 static_assert(compiling::gen_factorial<long long, 20>()[19] == 121645100408832000ll);90 // 991 is a prime91 static_assert(compiling::gen_prime<1000>()[991] == true);92 // const string concat93 static_assert(std::is_same_v<concat<symbol("Hello"), symbol(" "), symbol("world"), symbol("!")>, symbol("Hello world!")>);94}支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!
最后更新于 2023-08-07
部分内容可能已过时
站点统计
文章
45
分类
5
标签
51
总字数
42,438
运行时长
0 天
最后活动
0 天前
站点信息
站点域名blog.truebigsand.top
Fireflyv6.13.5
Astrov7.0.2
Nodev22.23.0
pnpmv9.14.4
构建时间2026年7月1日 02:21:34
系统信息Linux / x86_64
日
一
二
三
四
五
六
文章目录
公告
你好
音乐
音乐
暂未播放
0:00/0:00
暂无歌词
分类
标签
站点统计
文章
45
分类
5
标签
51
总字数
42,438
运行时长
0 天
最后活动
0 天前
站点信息
站点域名blog.truebigsand.top
Fireflyv6.13.5
Astrov7.0.2
Nodev22.23.0
pnpmv9.14.4
构建时间2026年7月1日 02:21:34
系统信息Linux / x86_64
真-大沙子