title: C++ Template Specialization with Constant Value
body: <p>Is there a straightforward way for defining a partial specialization of a C++ template class given a numerical constant for one of the template parameters? I'm trying to create special constructors for only certain kinds of template combinations:</p>
  
  <pre><code>template &lt;typename A, size_t B&gt; class Example
  {
      public:
          Example() { };
  
          A value[B];
  };
  
  template &lt;typename A, 2&gt; class Example
  {
      public:
          Example(b1, b2) { value[0] = b1; value[1] = b2; };
  };
  </code></pre>
  
  <p>This example won't compile, returning an error <code>Expected identifier before numeric constant</code> in the second definition.</p>
  
  <p>I've had a look through a number of examples here and elsewhere, but most seem to revolve around specializing with a type and not with a constant.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Looking for a way to write a conditionally used constructor, something functionally like this:</p>
  
  <pre><code>template &lt;typename A, size_t B&gt; class Example
  {
      public:
          // Default constructor
          Example() { };
  
          // Specialized constructor for two values
          Example&lt;A,2&gt;(A b1, A b2) { value[0] = b1; value[1] = b2; };
  
          A foo() {
            A r;
  
            for (size_t i = 0; i &lt; b; ++b)
              r += value[i];
  
            return r;
          }
  
          // Hypothetical specialized implementation
          A foo&lt;A, 2&gt;() {
            return value[0] + value[1];
          }
  
          A value[B];
  };
  </code></pre>