I have to object to a definition of logarithm that says that the logarithm of 0 is 0. You should define the logarithm of 1 as 0, simplify your formula (no need for the funky `!!') and let the compiler complain if someone tries to evaluate the logarithm of 0.
I did that to keep things really simple. How's the following?
// b = base, n = number, r = recursion depth level
template <unsigned int b, unsigned int n, unsigned int r = 0, bool recurse = n >= b, bool valid = (n > 0) && (b > 1)>
struct log;
template <unsigned int b, unsigned int n, unsigned int r, bool recurse>
struct log<b, n, r, recurse, true>
{
enum { value = log<b, n / b, r + 1>::value };
};
template <unsigned int b, unsigned int n, unsigned int r>
struct log<b, n, r, false, true>
{
enum { value = r };
};
I'm not sure how much I like the above. Is there a better way to do it?