title: clang vs gcc run-time difference: c++ class template built w clang crashes w/o copy constructor, built w gcc crashes with copy constructor
body: <p>Would does the following code crash during run-time when built with clang (but not gcc) unless I include a copy constructor for the <code>TableTypeCarrier</code> template class? If I include that copy constructor why do I experience the same run-time crash when built with gcc?</p>
  
  <p>I have the following c++ classes defined:</p>
  
  <pre><code>class TableTypeCarrierBase {};
  
  template&lt;class T&gt;
  class TableTypeCarrier: public TableTypeCarrierBase {
  public:
      TableTypeCarrier(const T * const p) : m_p(p) {}
  
  #ifdef __clang__
      TableTypeCarrier(const TableTypeCarrier&lt;T&gt;&amp; o) : m_p(o.m_p) {}
  #endif
  
      const T * Get() const
      {
        return m_p;
      }
  
  private:
      const T * const m_p;
  };
  
  struct PsiTable {
      PsiTable() : m_priv(NULL) { }
  
      template&lt;typename T&gt; void Set(const TableTypeCarrier&lt;T&gt; inT)
      {
        m_priv = &amp;inT;
      }
  
      template&lt;typename T&gt; const T * Get() const
      {
        return (!m_priv) ? NULL : ((TableTypeCarrier&lt;T&gt;*)m_priv)-&gt;Get(); 
      }
  
  private:
      const TableTypeCarrierBase *m_priv;
  };
  </code></pre>
  
  <p>You'll notice that the copy constructor of the <code>TableTypeCarrier</code> class template is conditionalized to only be built by clang and not gcc.</p>
  
  <p>While writing this code originally (without the above conditionalized copy constructor), I was only using gcc to build and test it.  When I tried to test it built with clang, the build would always succeed, but there would be a run-time crash while manipulating objects of these classes.</p>
  
  <p>I spent months debugging this.  I don't exactly recall the path that led me to try adding a copy constructor to the <code>TableTypeCarrier</code> template class, but indeed that solved the problem with my binaries built using clang.  Unfortunately, simply having this copy constructor declared seemed to cause the same problem as originally described, but only when built using gcc.</p>
  
  <p>Naturally, I added precompiler directives to conditionalize the copy constructor only if built using clang.</p>
  
  <p>This fix worked for me, but I want to understand why.</p>
  
  <p>If you need context to fully understand the problem, here is a link to the header that defines these classes within the full project at a point in history when this exact code was still present:</p>
  
  <p><a href="https://github.com/mkrufky/libdvbtee/blob/v0.4.0/libdvbtee/decode/table/table.h" rel="nofollow">https://github.com/mkrufky/libdvbtee/blob/v0.4.0/libdvbtee/decode/table/table.h</a></p>
  
  <p>I have since refactored the code and removed the <code>TableTypeCarrierBase</code> class and <code>TableTypeCarrier</code> template classes alltogether, as these were unnecessary proxies.  Despite removing this code, I still want to understand why binaries built with clang vs binaries built with gcc behave differently when <code>TableTypeCarrier</code> has (or lacks) a copy constructor.</p>
  
  <p>Why does a clang-built binary require this copy constructor?  Why does a gcc-built binary crash if I include it?</p>