title: c++ power of integer, template meta programming
body: <p>I want to make a function which returns a power of integer.
  Please read the fmuecke's solution in 
  <a href="https://stackoverflow.com/questions/1505675/power-of-an-integer-in-c">power of an integer in c++</a> .</p>
  
  <p>However, I want to generalize his solution to the arbitrary type T.
  Since c++11 has constexpr, I guess this is possible.</p>
  
  <p>Naively, I tried something like, </p>
  
  <pre><code>template&lt;class T, int N&gt;
  inline constexpr T pow(const T x){
      return pow&lt;N-1&gt;(x) * x;
  }
  template&lt;class T&gt;
  inline constexpr T pow&lt;T, 1&gt;(const T x){
      return x;
  }
  template&lt;class T&gt;
  inline constexpr T pow&lt;T, 0&gt;(const T x){
      return 1;
  }
  </code></pre>
  
  <p>Actually this approach failed since the partial specialization for function template is not allowed.</p>
  
  <p>And one more question. I heard that it is up to the compiler whether the constexpr function is evaluated in compile time or not.
  How do I force it to compute for general type.
  I read from somewhere that one of the simplest hack for integral consts is to wrap it in std::integral_const::value. </p>