title: Using "constexpr" to use string literal for template parameter
body: <p>I have written some code to cast <code>const char*</code> to <code>int</code> by using <code>constexpr</code> and thus I can use a <code>const char*</code> as a template argument. Here is the code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  class conststr
  {
      public:
          template&lt;std::size_t N&gt;
          constexpr conststr(const char(&amp;STR)[N])
          :string(STR), size(N-1)
          {}
  
          constexpr conststr(const char* STR, std::size_t N)
          :string(STR), size(N)
          {}
  
          constexpr char operator[](std::size_t n)
          {
              return n &lt; size ? string[n] : 0;
          }
  
          constexpr std::size_t get_size()
          {
              return size;
          }
  
          constexpr const char* get_string()
          {
              return string;
          }
  
          //This method is related with Fowler–Noll–Vo hash function
          constexpr unsigned hash(int n=0, unsigned h=2166136261)
          {
              return n == size ? h : hash(n+1,(h * 16777619) ^ (string[n]));
          }
  
      private:
          const char* string;
          std::size_t size;
  };
  
  // output function that requires a compile-time constant, for testing
  template&lt;int N&gt; struct OUT
  {
      OUT() { std::cout &lt;&lt; N &lt;&lt; '\n'; }
  };
  
  int constexpr operator "" _const(const char* str, size_t sz)
  {
      return conststr(str,sz).hash();
  }
  
  int main()
  {
      OUT&lt;"A dummy string"_const&gt; out;
      OUT&lt;"A very long template parameter as a const char*"_const&gt; out2;
  }
  </code></pre>
  
  <p>In this example code, type of <code>out</code> is <code>OUT&lt;1494474505&gt;</code> and type of <code>out2</code> is <code>OUT&lt;106227495&gt;</code>. Magic behind this code is <code>conststr::hash()</code> it is a <code>constexpr</code> recursion that uses <a href="http://isthe.com/chongo/tech/comp/fnv/" rel="noreferrer">FNV Hash function</a>. And thus it creates an integral hash for const char* which is hopefully a unique one.
  <br><br>
  I have some questions about this method:<br></p>
  
  <ol>
  <li>Is this a safe approach to use? Or can this approach be an evil in a specific use?</li>
  <li>Can you write a better hash function that creates different integer for each string without being limited to a number of chars? (in my method, the length is long enough)</li>
  <li>Can you write a code that implicitly casts <code>const char*</code> to <code>int constexpr</code> via <code>conststr</code> and thus we will not need aesthetically ugly (and also time consumer) <code>_const</code> user-defined string literal? For example <code>OUT&lt;"String"&gt;</code> will be legal (and cast "String" to integer).</li>
  </ol>
  
  <p>Any help will be appreciated, thanks a lot.</p>