title: Does the anonymous namespace enclose all namespaces?
body: <p>In C++ you specify internal linkage by wrapping your class and function definitions inside an anonymous namespace. You can also explicitly instantiate templates, but to be standards conforming any explicit instantiations of the templates must occur in the same namespace. AFAICT this should compile, but GCC fails on it:</p>
  
  <pre><code>namespace foo {
  
  template&lt;class T&gt;
  class bar {};
  
  }
  
  using namespace foo;
  
  namespace {
  template class bar&lt;int&gt;;
  }
  
  int main()
  {
      return 0;
  }
  </code></pre>
  
  <p>With the error:</p>
  
  <pre><code>namespace_test.cpp:11: error: explicit instantiation of 'class bar&lt;int&gt;' in namespace '&lt;unnamed&gt;' (which does not enclose namespace 'foo')
  </code></pre>
  
  <p>Which is interesting because the anonymous namespace should just be specifying linkage, not really functioning as a namespace, and the global namespace definitely encloses foo, since it encloses every namespace. But even this doesn't work!:</p>
  
  <pre><code>template&lt;class T&gt;
  class bar {};
  
  using namespace foo;
  
  namespace {
  template class bar&lt;int&gt;;
  }
  
  int main()
  {
      return 0;
  }
  </code></pre>
  
  <p>Which fails with the same error, just listing the global namespace instead:</p>
  
  <pre><code>namespace_test.cpp:11: error: explicit instantiation of 'class bar&lt;int&gt;' in namespace '&lt;unnamed&gt;' (which does not enclose namespace '::')
  </code></pre>
  
  <p>:/</p>