title: Separate Compilation of template with non-type argument
body: <p>I'm trying to do separate compilation of my non-type argument template, but I am facing a few problems.  My application has a number of opcodes, and each opcode is associated with certain data and functions, many of which are similar and could be defined in the primary template, then the differences can be put into specialized templates. </p>
  
  <p>So here is the original in-line code:
  (header)</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  template&lt;int opcode&gt;
  class BAR  
  {
  public:
      BAR(){};
  
      void  foo()
      {
          std::cout &lt;&lt; "BAR&lt;int&gt;::foo()" &lt;&lt; std::endl; 
      } 
  }; 
  
  // specialize 1 and 2, otherwise we go with the
  // primary definition.
  template &lt;&gt;
  void BAR&lt;1&gt;::foo()
  {
      std::cout &lt;&lt; "BAR&lt;1&gt;::foo()" &lt;&lt; std::endl;
  }
  template &lt;&gt;
  void BAR&lt;2&gt;::foo()
  {
      std::cout &lt;&lt; "BAR&lt;2&gt;::foo()" &lt;&lt; std::endl;
  }  
  </code></pre>
  
  <p>I have a simple 'main':</p>
  
  <pre><code>int  main(int argc, char* argv[])
  { 
       BAR&lt;1&gt; bar_1;
       BAR&lt;2&gt; bar_2;
       BAR&lt;3&gt; bar_3;
  
       bar_1.foo();
       bar_2.foo();
       bar_3.foo();
  
       return 0;
  }
  </code></pre>
  
  <p>I managed to put the specializations for '1' and '2' into a cpp file and this is
  what I have:
  (header)</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  template&lt;int opcode&gt;
  class BAR  
  {
  public:
      BAR(){};
  
      void  foo()
      {
          std::cout &lt;&lt; "BAR&lt;int&gt;::foo()" &lt;&lt; std::endl; 
      } 
  };
  
  // specialize 1 and 2, otherwise we go with the
  // primary definition.
  template&lt;&gt; void BAR&lt;1&gt;::foo() ;
  template&lt;&gt; void BAR&lt;2&gt;::foo() ;
  </code></pre>
  
  <p>(cpp)</p>
  
  <pre><code>#include "Foo.h"
  #include &lt;iostream&gt;  
  
  template&lt;&gt; void BAR&lt;1&gt;::foo()
  {
      std::cout &lt;&lt; "BAR&lt;1&gt;::foo()" &lt;&lt; std::endl;
  } 
  template&lt;&gt; void BAR&lt;2&gt;::foo()
  {
      std::cout &lt;&lt; "BAR&lt;2&gt;::foo()" &lt;&lt; std::endl;
  } 
  void x()
  {
      BAR&lt;1&gt; b1;
      BAR&lt;2&gt; b2;
      b1.foo();
      b2.foo();
  }
  </code></pre>
  
  <p>I not real crazy about the x() function, but without this I get unresolved symbols for BAR&lt;1>::foo() &amp; BAR&lt;2>::foo(), if there's a better way, I'd be interested ( MSVC 13 compiler). </p>
  
  <p>Ok so far so good. I really want to put the primary definition of the function foo() into the CPP, but I can't seem to get the syntax right:</p>
  
  <pre><code>template&lt;&gt; void BAR&lt;int&gt;::foo()
  {
      std::cout &lt;&lt; "BAR&lt;int&gt;::foo()" &lt;&lt; std::endl; 
  }
  </code></pre>
  
  <p>this is not allowed by the compiler, and rightly so I guess,  is not
  a valid non-type value.  So what is the magic words to use to do this?  </p>
  
  <p>Many thanks in advance for any help.
  dave. </p>