title: Name conflict, unnamed namespace, class template
body: <p>I have a class template <code>Foo&lt;T&gt;</code>, deriving from <code>FooBase</code>.</p>
  
  <p><strong>Foo.h:</strong></p>
  
  <pre><code>class FooBase
  {
  public:
      virtual ~FooBase() {}
  };
  
  template &lt;class T&gt;
  class Foo : public FooBase
  {
  public:
      T t;
  };
  </code></pre>
  
  <p>Then, I have two translation units instantiating Foo with different <code>T</code>'s. The class used as template parameter is called <code>T</code> in both translation units, but protected by an unnamed namespace. Each translation unit defines a free function.</p>
  
  <p><strong>Test1.h:</strong></p>
  
  <pre><code>void test1();
  </code></pre>
  
  <p><strong>Test1.cpp:</strong></p>
  
  <pre><code>#include "Test1.h"
  #include "Foo.h"
  #include &lt;cassert&gt;
  
  namespace { class T {}; }
  
  void test1()
  {
      Foo&lt;T&gt; foo;
      FooBase &amp; base = foo;
      assert(&amp;base == &amp;foo); // To be able to breakpoint here
  }
  </code></pre>
  
  <p><strong>Test2.h:</strong></p>
  
  <pre><code>void test2();
  </code></pre>
  
  <p><strong>Test2.cpp:</strong></p>
  
  <pre><code>#include "Test2.h"
  #include "Foo.h"
  #include &lt;cassert&gt;
  
  namespace { class T { int x; }; }
  
  void test2()
  {
      Foo&lt;T&gt; foo;
      FooBase &amp; base = foo;
      assert(&amp;base == &amp;foo); // To be able to breakpoint here
  }
  </code></pre>
  
  <p>Finally, I have a main that calls both free function, and I link all three translation units.</p>
  
  <p><strong>main.cpp:</strong></p>
  
  <pre><code>#include "Test1.h"
  #include "Test2.h"
  
  int main()
  {
      test1();
      test2();
  }
  </code></pre>
  
  <p><strong>Question: Is this legal C++11 ?</strong></p>
  
  <p>I thought it should be since the name conflict is resolved by the unnamed namespace. However, I am now in doubt because:</p>
  
  <ol>
  <li>GDB ( v7.7.1, on Kubuntu 14.04 64bits) is really confused about this</li>
  <li>I have weird bugs in my real-life case which I can't track down</li>
  </ol>
  
  <p><strong>GDB</strong></p>
  
  <p>GDB issues those warnings on test2():</p>
  
  <pre><code>can't find linker symbol for virtual table for `FooBase' value
  can't find linker symbol for virtual table for `Foo&lt;(anonymous namespace)::T&gt;' value
  </code></pre>
  
  <p>And is not able to determine that <code>base</code> is actually of dynamic type <code>Foo</code> and therefore inspect its member <code>t</code>. Also, instead of the nice</p>
  
  <pre><code>&lt;vtable for Foo&lt;(anonymous namespace)::T&gt;+16&gt;
  </code></pre>
  
  <p>which I get on test1, I get the following on test2:</p>
  
  <pre><code>&lt;_ZTV3FooIN12_GLOBAL__N_11TEE+16&gt;
  </code></pre>
  
  <p>AND even much much worse, when inspecting <code>foo.t</code> on test1, it finds the member <code>foo.t.x</code> which should only exist in test2!</p>
  
  <p>See below for screenshots within QtCreator:</p>
  
  <p><a href="https://i.stack.imgur.com/5KDaY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5KDaY.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/u1Y8N.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/u1Y8N.png" alt="enter image description here"></a></p>
  
  <p>All above issues are resolved if I name the template parameter <code>T1</code> in Test1.cpp and <code>T2</code> in Test2.cpp.</p>
  
  <p>Despite GDB confusion, on all variants I tried around this minimal example, the program always seems to behave correctly (GCC 4.8.2). For instance printing sizeof(T) via a virtual method called from base correctly returns <code>1</code> and <code>4</code> for test1 and test2 respectively (while it prints <code>1</code> and <code>1</code> if I remove the unnamed namespace, due to the actual name conflict, which I know makes the code not legal C++).</p>
  
  <p><strong>Real-life case</strong></p>
  
  <p>On my real-life case, I have a segfault that:</p>
  
  <ol>
  <li>always occurs with "same name in global scope" (obviously)</li>
  <li>occurs unpredictably with "same name under unnamed namespace"</li>
  <li>never occurred with manually assigned unique names</li>
  </ol>
  
  <p>I don't know if it's because assigning unique names manually fix the issue (could it, since they were already in unnamed namespace??), or if my code is still broken somewhere else and I'm just "lucky" (i.e., a hidden undefined behavior, which is very scary). I've spent two days trying to reduce to a minimal example that still crashes this way, but failed. I only managed to get minimal examples that work, or my actual code that crashes sometimes.</p>