title: C++ template nontype parameter arithmetic
body: <p>I am trying to specialize template the following way:</p>
  
  <pre><code>template&lt;size_t _1,size_t _2&gt; // workaround: bool consecutive = (_1 == _2 - 1)&gt;
  struct integral_index_ {};
  ...
  template&lt;size_t _1&gt;
  struct integral_index_&lt;_1, _1 + 1&gt; { // cannot do arithmetic?
  //struct integral_index_&lt;_1, _2, true&gt; { workaround
  };
  </code></pre>
  
  <p>however I get compiler message error</p>
  
  <pre><code>the template argument list of the partial specialization includes a non
  -type argument whose type depends on a template parameter.
  </code></pre>
  
  <p>what do my doing wrong?
  thanks</p>
  
  <p>I put workaround in comments.  Apparently I cannot do arithmetic in template specialization?  seems counterintuitive.</p>
  
  <p>here is my final solution in the problem to be solved. Basically, consecutive index requires one multiplication only.</p>
  
  <pre><code>130 template&lt;size_t _1,size_t _2, bool consecutive = (_1 == _2 - 1)&gt;
  131 struct integral_index_ {
  132     template&lt;typename T, typename U&gt;
  133     __device__
  134     static T eval(const T (&amp;N)[4], const U &amp;index) {
  135         T j = index/N[_1];
  136         return ((index - j*N[_1])*range&lt;0,_1&gt;::multiply(N) +
  137                 j*range&lt;0,_2&gt;::multiply(N));
  138     }
  139 };
  140
  141 template&lt;size_t _1,size_t _2&gt;
  142 struct integral_index_&lt;_1, _2, true&gt; {
  143     template&lt;typename T, typename U&gt;
  144     __device__
  145     static T eval(const T (&amp;N)[4], const U &amp;index) {
  146         return index*range&lt;0,_1&gt;::multiply(N);
  147     }
  148 };
  149
  150 template&lt;size_t _1,size_t _2, typename T, typename U&gt;
  151 __device__
  152 T integral_index(const T (&amp;N)[4], const U &amp;index) {
  153     return integral_index_&lt;_1,_2&gt;::eval(N, index);
  154 }
  </code></pre>