Works with any value you can provide as an unsigned long template argument, in any base:
template<unsigned B, unsigned long N>
struct base_digits_detail {
enum { result = 1 + base_digits_detail<B, N/B>::result };
};
template<unsigned B>
struct base_digits_detail<B, 0> {
private:
enum { result = 0 };
template<unsigned, unsigned long>
friend class base_digits_detail;
};
template<unsigned B, unsigned long N>
struct base_digits {
enum { result = base_digits_detail<B, N>::result };
};
template<unsigned B>
struct base_digits<B, 0> {
enum { result = 1 };
};
Test
#include <climits>
#include <iostream>
int main() {
std::cout << base_digits<10, 0>::result << '\n';
std::cout << base_digits<10, 1>::result << '\n';
std::cout << base_digits<10, 10>::result << '\n';
std::cout << base_digits<10, 100>::result << '\n';
std::cout << base_digits<10, 1000>::result << '\n';
std::cout << base_digits<10, UINT_MAX>::result << '\n';
std::cout << '\n';
std::cout << base_digits<8, 0>::result << '\n';
std::cout << base_digits<8, 01>::result << '\n';
std::cout << base_digits<8, 010>::result << '\n';
std::cout << base_digits<8, 0100>::result << '\n';
std::cout << base_digits<8, 01000>::result << '\n';
std::cout << base_digits<8, UINT_MAX>::result << '\n';
return 0;
}
1
1
2
3
4
10
1
1
2
3
4
11