Literals.hpp 861 Bytes
Newer Older
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
#pragma once

#include "Basic.hpp"

namespace AMDiS
{
  // inspired by Boost.hana
  
  namespace Impl 
  {
    constexpr unsigned char2digit(const char c) 
    {
      assert(c >= '0' && c <= '9' && "Unknown digit in integral constant");
      return unsigned(c) - unsigned('0');
    }

    template <size_t N>
    constexpr size_t string2num(const char (&arr)[N]) 
    {
      assert(arr[0] != '-' && "Negative integral constant");

      size_t result = 0;
      size_t power  = 1;

      for (size_t i = 0; i < N; ++i) {
          char c = arr[N - 1 - i];
          result+= char2digit(c) * power;
          power *= 10u;
      }

      return result;
    }
    
  } // end namespace Impl

  template <char... digits>
  constexpr auto operator"" _c()
  { 
    return index_<Impl::string2num<sizeof...(digits)>({digits...})>(); 
  }

} // end namespace AMDiS