title: Several examples about template partial ordering to which GCC and Clang disagree
body: <p>The first example is</p>
  <pre class="lang-cpp prettyprint-override"><code>template &lt;class... T&gt; struct V {}; 
  template &lt;class... Ts, class... Us&gt; void Foo(V&lt;Ts...&gt;, V&lt;Us&amp;...&gt;) {} // #3 
  template &lt;class... Us&gt; void Foo(V&lt;&gt;, V&lt;Us&amp;...&gt;) {}                   // #4 
  void h() { 
     Foo(V&lt;&gt;(), V&lt;&gt;()); 
  } 
  </code></pre>
  <p>The above example is mentioned in <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#2235" rel="nofollow noreferrer">CWG2235</a>. In that paper, which says <code>#4</code> is a better candidate. However, GCC and Clang have <a href="https://godbolt.org/z/sb3bxMPce" rel="nofollow noreferrer">disagreements</a> with this example. I think GCC is correct, my reason is, Take <code>#4</code> as argument template and <code>#3</code> as parameter template, the deductions are successful for each pair that is used to determine the ordering.<br />
  Instead, when take <code>#3</code> as argument template and <code>#4</code> as parameter template, the following rules is applied here<br />
  <a href="https://timsong-cpp.github.io/cppwp/n4861/temp.deduct.type#9" rel="nofollow noreferrer">temp.deduct.type#9</a></p>
  <blockquote>
  <p>During partial ordering, if A<sub>i</sub> was originally a pack expansion:</p>
  <blockquote>
  <ul>
  <li>if P does not contain a template argument corresponding to A<sub>i</sub> then A<sub>i</sub> is ignored;</li>
  <li>otherwise, if P<sub>i</sub> is not a pack expansion, template argument deduction fails.</li>
  </ul>
  </blockquote>
  </blockquote>
  <p>That means the deduction is still successful for each pair. Finally, there's no special rule in section [temp.deduct.partial] to determine which is more specialized. Hence, it should be ambiguous here which should be GCC's opinion.</p>
  <p>For this example, if I have some misunderstanding, please point it out.</p>
  <hr />
  <p>Now, consider a bit complex example that GCC and Clang also disagree.</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  struct A { };
  template&lt;class T&gt; 
  struct B {
      template&lt;class R&gt; 
      int operator*(R) {
          std::cout&lt;&lt;&quot;#1\n&quot;;
          return 0;  
      }
  };
  
  template&lt;class R&gt; 
  int operator*(B&lt;A&gt;&amp;&amp;, R){
    std::cout&lt;&lt;&quot;#2\n&quot;;
    return 0;
  }
  int main(){
      B&lt;A&gt;{} * 0;
  }
  </code></pre>
  <p>GCC considers the example as ambiguous while Clang chooses <code>#1</code>. This time, I'm still a proponent of GCC. My reason is<br />
  <a href="https://timsong-cpp.github.io/cppwp/n4861/temp.func.order#3" rel="nofollow noreferrer">temp.func.order#3</a></p>
  <blockquote>
  <p>For a function template M with cv-qualifiers cv that is a member of a class A:</p>
  <blockquote>
  <ul>
  <li>The type X(M) is “rvalue reference to cv A” if the optional ref-qualifier of M is &amp;&amp; or if M has no ref-qualifier and the positionally-corresponding parameter of the other transformed template has rvalue reference type; if this determination depends recursively upon whether X(M) is an rvalue reference type, it is not considered to have rvalue reference type.</li>
  </ul>
  </blockquote>
  </blockquote>
  <p>It means that the implicit parameter of <code>#1</code> should be <code>B&lt;A&gt;&amp;&amp;</code> during the ordering partial. Which is identical with the first parameter in <code>#2</code> and the other pair can also be deduced successfully in both directions. Hence, <code>#1</code> and <code>#2</code> should be ambiguous here too.</p>
  <p>In general, in these two examples, I agree with the result processed by GCC. If I misunderstand something, please point it out.</p>