1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
template <bool Test, typename Rtrue, typename Rfalse>
struct if_
{
enum { value = Rtrue::value };
};
template <typename Rtrue, typename Rfalse>
struct if_<false, Rtrue, Rfalse>
{
enum { value = Rfalse::value };
};
template <int N>
struct number
{
enum { value = N };
};
template <int N>
struct nb_digits_r
{
enum { value = 1 + nb_digits_r<N / 10>::value };
};
template<>
struct nb_digits_r<0>
{
enum { value = 0 };
};
template <int N>
struct nb_digits
{
enum { value = nb_digits_r<N>::value };
};
template<>
struct nb_digits<0>
{
enum { value = 1 };
};
template <int N, int Digit>
struct nth_digit_r
{
enum { value = nth_digit_r<N / 10, Digit - 1>::value };
};
template <int N>
struct nth_digit_r<N, 0>
{
enum { value = N % 10 + '0' };
};
template <int N, int Digit>
struct nth_digit
{
enum { value = if_<nb_digits<N>::value <= Digit, number<0>, nth_digit_r<N, nb_digits<N>::value - Digit - 1>>::value };
};
template <int ID>
struct to_str
{
static const char value[15];
};
// work with up to 5 digits add until nth_digit<ID, N> for more
template <int ID>
const char to_str<ID>::value[15] = {nth_digit<ID, 0>::value, nth_digit<ID, 1>::value, nth_digit<ID, 2>::value, nth_digit<ID, 3>::value, nth_digit<ID, 4>::value, 0};
|