I made a simple sqrt struct using TMP. It goes like :
template <int N, int i>
struct sqrt {
static const int val = (i*i <= N && (i + 1)*(i + 1) > N) ? i : sqrt<N, i - 1 >::val;
};
but is causes error since it does not have the exit condition, so I added this :
template <int N>
struct sqrtM<N, 0> {
static const int val = 0;
};
So as I understand it, in case we use TMP, the compiler goes into recursion loop until they meet the exit condition (in terms of sqrt, usally when i = 0 or i = 1)
But if we make a recursive sqrt function, compiler doesn't have to dive until it meets i = 0, because at some point, recursive function ends at exact location where condition (i*i <= N && (i + 1)*(i + 1) > N) is met.
So let's assume we put very large value into our sqrt, then our TMP should do extra computation of sqrt<N, sqrt<N-1>::val> compared to the recursive version of sqrt function, and it seems waste to me.
Am I getting it wrong? Or TMP is really worth it even in this kind of cases?