Suppose we want to build a non-trivial table at compile time
template<int N, int M>
constexpr auto foo()
{
std::array<std::array<int, N>, M> a = {};
for(int m = 1; m < M; m++)
for(int n = 1; n < N; n++)
{
// For exposition only
auto x = (m ^ 42) + (n << 3) - m;
auto y = (n ^ 420) + (m % 420);
a[m][n] = (a[(x + m) % m][(y + n) % n] + (x ^ y)) % 0xFACADE;
}
return a;
}
constexpr auto bar(int n, int m)
{
constexpr auto dim = /* something */;
constexpr auto table = foo<dim, dim>();
return table[n][m];
}
It doesn't take much to push compile times over the top. The alternative is to generate the table as source code via a script, which is obviously much less nice.
How do I reduce the compile times such functions?
Some motivation
constexpr functions differ hugely from regular functions, evident by the extreme slowness compared to regular functions. Aside from being executed by the compiler, they have bounds checking, overflow checking and pretty much all UB preventing checks in place. I suspect this renders most intuition gathered from regular functions useless.