title: Specialization of template after instantiation?
body: <p>My full code is too long, but here is a snippet that will reflect the essence of my problem:</p>
  
  <pre><code>class BPCFGParser {
    public:
  
    ...
    ...
  
    class Edge {
      ...
      ...
    };
  
  
    class ActiveEquivClass {
      ...
      ...
    };
  
    class PassiveEquivClass {
      ...
      ...
    };
  
    struct EqActiveEquivClass {
      ...
      ...
    };
  
    struct EqPassiveEquivClass {
      ...
      ...
    };
  
  
  
    unordered_map&lt;ActiveEquivClass, Edge *, hash&lt;ActiveEquivClass&gt;, EqActiveEquivClass&gt; discovered_active_edges;
    unordered_map&lt;PassiveEquivClass, Edge *, hash&lt;PassiveEquivClass&gt;, EqPassiveEquivClass&gt; discovered_passive_edges;
  
  };
  
  namespace std {
  
  
  template &lt;&gt;
  class hash&lt;BPCFGParser::ActiveEquivClass&gt;
  {
  
      public:
          size_t operator()(const BPCFGParser::ActiveEquivClass &amp; aec) const {
  
          }
  };
  
  template &lt;&gt;
  class hash&lt;BPCFGParser::PassiveEquivClass&gt;
  {
  
      public:
          size_t operator()(const BPCFGParser::PassiveEquivClass &amp; pec) const {
  
          }
  };
  
  }
  </code></pre>
  
  <p>When I compile this code, I get the following errors:</p>
  
  <pre><code>In file included from BPCFGParser.cpp:3,
                   from experiments.cpp:2:
  BPCFGParser.h:408: error: specialization of ‘std::hash&lt;BPCFGParser::ActiveEquivClass&gt;’     after instantiation
  BPCFGParser.h:408: error: redefinition of ‘class                 std::hash&lt;BPCFGParser::ActiveEquivClass&gt;’
  /usr/include/c++/4.3/tr1_impl/functional_hash.h:44: error: previous definition of     ‘class std::hash&lt;BPCFGParser::ActiveEquivClass&gt;’
  BPCFGParser.h:445: error: specialization of     ‘std::hash&lt;BPCFGParser::PassiveEquivClass&gt;’ after instantiation
  BPCFGParser.h:445: error: redefinition of ‘class std::hash&lt;BPCFGParser::PassiveEquivClass&gt;’
  /usr/include/c++/4.3/tr1_impl/functional_hash.h:44: error: previous definition of     ‘class std::hash&lt;BPCFGParser::PassiveEquivClass&gt;’
  </code></pre>
  
  <p>Now I have to specialize std::hash for these classes (because standard std::hash definition does not include user defined types). When I move these template specializations before the definition of class <code>BPCFGParser</code>, I get a variety of errors for a variety of different things tried, and somewhere (<a href="http://www.parashift.com/c++-faq-lite/misc-technical-issues.html" rel="noreferrer">http://www.parashift.com/c++-faq-lite/misc-technical-issues.html</a>) I read that: <blockquote> Whenever you use a class as a template parameter, the declaration of that class must be complete and not simply forward declared.  </blockquote></p>
  
  <p>So I'm stuck. I cannot specialize the templates after <code>BPCFGParser</code> definition, I cannot specialize them before <code>BPCFGParser</code> definition, how may I get this working?</p>
  
  <hr>
  
  <blockquote>
    <p>You need to move the specialization into an inner class inside of BPCFGParser. Doing so meets both requirements.</p>
  </blockquote>
  
  <p>Thank you very much for the answer :)</p>
  
  <p><code>hash</code> class is defined within the namespace <code>std</code>. It does not allow me to specialize the templates for <code>hash</code> in a non-namespace scope. Even the following:</p>
  
  <pre><code>template &lt;&gt;
    class std::hash&lt;ActiveEquivClass&gt; {
  ...
  </code></pre>
  
  <p>did not work. When I enclose the specializations with <code>namespace std {}</code>, however, it gives the weird error of:</p>
  
  <pre><code>In file included from BPCFGParser.cpp:3,
                   from experiments.cpp:2:
  BPCFGParser.h:225: error: expected unqualified-id before ‘namespace’
  experiments.cpp:7: error: expected `}' at end of input
  BPCFGParser.h:222: error: expected unqualified-id at end of input
  </code></pre>
  
  <p>In an answer given in <a href="http://www.velocityreviews.com/forums/t672186-namespace-within-class.html" rel="noreferrer">velocityreviews</a>, someone claims that namespaces cannot be defined within classes. So I'm still stuck.</p>