What should I do to check whether an integer array is palindrome (e.g. 1 3 10 3 1 is palindrome) in compile time?
A possible framework could be:
template <int...>
class IntArray;
template <int... values>
class Palindrome<IntArray<values...>>;
static_assert(Palindrome<IntArray<1, 2, 1>>::check == true);
I know this question seems to be meaningless but I'm just curious about the grammar that I should use.
I've read posts and blogs about compile-time Fibonacci calculation but found nothing inspiring.
Could anyone tell me how to implement this?
I know little about compile-time programming; the most complicated problem that I can tackle with compile-time programming is like this:
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 <>
struct Fibonacci<1>
{
enum
{
value = 1
};
};
template <>
struct Fibonacci<0>
{
enum
{
value = 0
};
};