title: Is instantiating a class template with an incomplete type ill-formed, if the type is defined afterwards?
body: <p>This code is surely ill-formed, because <code>Foo</code> is specialized after an instantiation point:</p>
  
  <pre><code>template &lt;typename T&gt;
  struct Foo {
      int a;
  };
  
  Foo&lt;int&gt; x = { 42 };
  
  template &lt;&gt;
  struct Foo&lt;int&gt; {
      const char *a;
  };
  
  Foo&lt;int&gt; x = { "bar" };
  </code></pre>
  
  <p>It is ill formed because of the part of the <a href="http://eel.is/c++draft/temp.point#8" rel="noreferrer">standard</a> I've put emphasis:</p>
  
  <blockquote>
    <p>A specialization for a function template, a member function template, or of a member function or static data member of a class template may have multiple points of instantiations within a translation unit, and in addition to the points of instantiation described above, for any such specialization that has a point of instantiation within the translation unit, the end of the translation unit is also considered a point of instantiation. A specialization for a class template has at most one point of instantiation within a translation unit. A specialization for any template may have points of instantiation in multiple translation units. <strong>If two different points of instantiation give a template specialization different meanings according to the one-definition rule, the program is ill-formed, no diagnostic required.</strong></p>
  </blockquote>
  
  <p>Now, is <strong>this</strong> code ill-formed?</p>
  
  <pre><code>struct A;
  
  template &lt;typename&gt; class Foo { };
  
  Foo&lt;A&gt; foo; // note A is incomplete here
  
  struct A {};
  </code></pre>
  
  <p>Does the ill-formedness change, if <code>Foo</code> declared like this?</p>
  
  <pre><code>struct A;
  
  template &lt;typename T&gt;
  struct Foo {
      Foo() {
          new T;
      }
  };
  
  Foo&lt;A&gt; foo; // note A is incomplete here
  
  struct A {};
  </code></pre>
  
  <p>I asked this question, because of the discussion under this <a href="https://stackoverflow.com/questions/52180744/pimpl-why-can-make-unique-be-called-on-an-incomplete-type">question</a>.</p>
  
  <p>Note, this is not a duplicate. That question is about why the code compiles, this question is about whether it is ill-formed. They differ, because an ill-formed program isn't necessarily a non-compiling program.</p>
  
  <hr>
  
  <p>Note, with clang and gcc, my example with <code>new T</code> compiles, while this example (<code>T</code> as a member) doesn't:</p>
  
  <pre><code>struct A;
  
  template &lt;typename T&gt;
  struct Foo {
      T t;
  };
  
  Foo&lt;A&gt; foo; // note A is incomplete here
  
  struct A {};
  </code></pre>
  
  <p>Maybe both are ill-formed, and diagnostic is given only for this last case?</p>