我应该如何在编译时检查整数数组是否是回文(例如 1 3 10 3 1 是回文)?
一个可能的框架可能是:
template <int...>
class IntArray;
template <int... values>
class Palindrome<IntArray<values...>>;
static_assert(Palindrome<IntArray<1, 2, 1>>::check == true);
Run Code Online (Sandbox Code Playgroud)
我知道这个问题似乎毫无意义,但我只是好奇我应该使用的语法。
我读过有关编译时斐波那契计算的帖子和博客,但没有发现任何启发性的内容。
谁能告诉我如何实现这个?
我对编译时编程知之甚少;我可以通过编译时编程解决的最复杂的问题是这样的:
template<int a, int b>
struct max_template {
static constexpr int value = a > b ? a : b;
};
constexpr int max_fun(int a, int b) {
return a > b ? a : b;
}
// or
template <unsigned N>
struct Fibonacci
{
enum
{
value = Fibonacci<N-1>::value + Fibonacci<N-2>::value
};
};
template …Run Code Online (Sandbox Code Playgroud)