title: Why are function template specializations not allowed inside a class?
body: <p>After having found answers to many of my questions on stackoverflow, I have now come up against a question of which I can't find the answer and I hope that someone is willing to help me!</p>
  
  <p>My problem is that I want to do an explicit templatization of a function inside a class in C++. My compiler (g++) and a look in the C++ standard (§14.7.3) tells me that this specialization has to be done in the namespace in which the class is declared. I understand that this implies that I cannot put the specialization inside the class, but I don't see the point of this restriction! Does anyone know if there is a good reason for not letting the specializations be made inside the class?</p>
  
  <p>I know that there are workarounds, e.g. to put the function inside a struct, but I want to understand why the language has this design. If there is a good reason for not allowing specialized functions inside the class, I guess I should know it before trying to work around it.</p>
  
  <p>Thanks in advance!</p>
  
  <hr>
  
  <p>To make my question a little bit more precise: Here is some code from a test example which illustrates what I want to do:</p>
  
  <pre><code>#include &lt;cstdio&gt;
  
  namespace MalinTester {
  
  template &lt;size_t DIMENSIONALITY&gt;
  class SpecializationTest {
  public:
      SpecializationTest() {
          privateVariable = 5;
      };
      virtual ~SpecializationTest() {};
  
      void execute() {
          execute&lt;DIMENSIONALITY&gt;();
      };
  
  private:
      int privateVariable;
      template &lt;size_t currentDim&gt;
      static void execute() {
          printf("This is the general case. Current dim is %d. The private variable is %d.\n", currentDim, privateVariable);
          execute&lt;currentDim-1&gt;();
      }
  
      template &lt;&gt;
      static void execute&lt;0&gt;() {
          printf("This is the base case. Current dim is 0.\n");
      }
  
  };
  </code></pre>
  
  <p>This is not possible; g++ says:</p>
  
  <pre><code>SpecializationTest_fcn.h:27: error: explicit specialization in non-namespace scope ‘class MalinTester::SpecializationTest&lt;DIMENSIONALITY&gt;’
  SpecializationTest_fcn.h:28: error: template-id ‘execute&lt;0&gt;’ in declaration of primary template
  </code></pre>
  
  <p>If I put the function execute outside the class, in the name space MalinTester, it will look like this:</p>
  
  <pre><code>#include &lt;cstdio&gt;
  
  namespace MalinTester {
  
      template &lt;size_t DIMENSIONALITY&gt; class SpecializationTest {};
  
      template &lt;size_t currentDim&gt;
      void execute() {
          printf("This is the general case. Current dim is %d. The private variable is %d.\n", currentDim, privateVariable);
          execute&lt;currentDim-1&gt;();
      }
  
      template &lt;&gt;
      void execute&lt;0&gt;() {
          printf("This is the base case. Current dim is 0.\n");
      }
  
      template &lt;size_t DIMENSIONALITY&gt;
      class SpecializationTest {
      public:
          SpecializationTest() {};
          virtual ~SpecializationTest() {};
  
          void execute() {
              MalinTester::execute&lt;DIMENSIONALITY&gt;();
          };
      private:
          int privateVariable = 5;
      };
  };
  };
  </code></pre>
  
  <p>and I cannot use privatevariable in the templatized versions of execute, as it is private in the class. I really <strong>want</strong> it private, as I want to have my data encapsulated as far as possible.</p>
  
  <p>Of course I can send privateVariable as an argument to the function, but I think it would be more beautiful to avoid this, and what I really wonder is if there is a good reason for the C++ standard not to allow explicit specialization as in the first code example above.</p>
  
  <hr>
  
  <p>@Arne Mertz: This is the workaround I have tried, but it doesn't allow using privateVariable either. And most of all, I wonder if it is a good idea to do like this. As I'm not allowed to make specializations of member functions, maybe I shouldn't do specializations of functions encapsulated in structs inside the class either.</p>
  
  <pre><code>#include &lt;cstdio&gt;
  
  namespace MalinTester {
  
  template &lt;size_t DIMENSIONALITY&gt;
  class SpecializationTest {
  public:
      SpecializationTest() {
          privateVariable = 5;
      };
      virtual ~SpecializationTest() {};
  
      void execute() {
          Loop&lt;DIMENSIONALITY, 0&gt;::execute();
      };
  
  private:
      int privateVariable;
  
      template &lt;size_t currentDim, size_t DUMMY&gt;
      struct Loop {
          static void execute() {
              printf("This is the general case. Current dim is %d.\n", currentDim);
              Loop&lt;currentDim-1, 0&gt;::execute();
          }
      };
  
      template &lt;size_t DUMMY&gt;
      struct Loop&lt;0, DUMMY&gt; {
          static void execute() {
              printf("This is the base case. Current dim is 0.\n");
          }
      };
  };
  };
  </code></pre>