<div dir="ltr"><div><div><font face="arial, helvetica, sans-serif">Hi,<br><br></font></div><font face="arial, helvetica, sans-serif">I was looking at Bug 19551.<br></font></div><font face="arial, helvetica, sans-serif"><br>
The test case is as follows :<br><br></font><pre><i><font face="arial, helvetica, sans-serif">template<typename T> auto f(T) { return 0; }
int k = f(0); // #1
template auto f(int); // #2
template int f(int); // #3</font></i></pre><pre><font face="arial, helvetica, sans-serif">Without line #1, clang accept #2 and reject #3.
With line #1, clang reject #2 and accept #3.<br><br></font></pre><pre><font face="arial, helvetica, sans-serif">I debug this code and found following :<br><br></font></pre><pre><font face="arial, helvetica, sans-serif">1. When the function template is instantiated (#1), clang visits functions<br>
   <br>
   <i>Sema::AddTemplateOverloadCandidate  <br>       Sema::DeduceTemplateArguments          <br>           Sema::FinishTemplateArgumentDeduction</i><br><br></font></pre><pre><font face="arial, helvetica, sans-serif">In <i>Sema::FinishTemplateArgumentDeduction</i> function, it creates a Specialization of the functiontemplate :<br>
<br><i>          Specialization = cast_or_null<FunctionDecl>(<br>                      SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,<br>                         MultiLevelTemplateArgumentList(*DeducedArgumentList)));</i><br>

</font></pre><pre><font face="arial, helvetica, sans-serif">The return type of the created Specialization is 'auto' at this point. After this, <br>it completes deduction of TemplateArguments and adds this Specialization as overloaded <br>
candidate. The return type still remains 'auto'.<br>
<br></font></pre><pre><font face="arial, helvetica, sans-serif">2. When clang process #2 (explicit instantiation), clang visits following function :<br><br>     <i>Sema::ActOnExplicitInstantiation<br>          Sema::DeduceTemplateArguments</i><br>
<i>               Sema::FinishTemplateArgumentDeduction   </i><br>
<br></font></pre><pre><font face="arial, helvetica, sans-serif">Again clang creates Specialization as in point 1 above, but strangely, <br>this time the return type of Specialization is 'int' though the return type of <br>
FunctionTemplate remains 'auto'. Since this return type 'int' does not match with <br>return type of explicit instantiation i.e. 'auto', the DeduceTemplateArguments return <br>failure.<br><br><br>        <br>
<i>    Sema::DeduceTemplateArguments{<br>          .................<br>          ................<br>
          ..................<br><br>         </i> <i>else if(!InOverloadResolution && !AT &&<br>            !Context.hasSameType(Specialization->getType(), ArgFunctionType)) //ArgFunctionType - auto <br>

                                                                              // SpecializationType - int<br>      return TDK_MiscellaneousDeductionFailure;<br><br>}<br><br></i></font></pre><pre><font face="arial, helvetica, sans-serif">Hence, we get error for line #2. For Line #3, the Specialization Type and ArgFunctionType <br>

both are 'int'<i>, </i>hence we get no error for Line #3.<br><br></font></pre><pre><font face="arial, helvetica, sans-serif">If there is no prior instantiation, Line #2 will succeed while Line #3 will throw error.<br>
<br></font></pre><pre><font face="arial, helvetica, sans-serif">Do we deduce return type during function template instantiation along with template parameters?</font></pre><pre><font face="arial, helvetica, sans-serif">( Which i guess we should not but we are probably doing it as evident as the return type of second Specialization becomes 'int')</font></pre>
<pre><font face="arial, helvetica, sans-serif"><br></font></pre><pre><font face="arial, helvetica, sans-serif">How should we prevent deduction of return type for second Specialization? (I tried a lot but couldn't find a way).</font></pre>
<pre><font face="arial, helvetica, sans-serif">GCC 4.8.2 is also behaving same as clang as described in the bug. <br></font></pre><pre><font face="arial, helvetica, sans-serif">Richard, any comment/help ? This bug was filed by you :)<br>
</font></pre><pre><font face="arial, helvetica, sans-serif">
</font></pre><div><div><div><div><font face="arial, helvetica, sans-serif"><br>-- <br>With regards,<br>Suyog Sarda</font><br>
</div></div></div></div></div>