<div dir="ltr">This is not endless recursion, just a stack overflow. I get:<div><br></div><div><div><stdin>:2:6: fatal error: recursive template instantiation exceeded maximum depth of 256</div><div>auto g(T x);</div>
<div> ^</div><div><stdin>:15:14: note: in instantiation of function template specialization 'f<double>' requested here</div><div> return x*f(x-1);</div><div> ^</div><div><stdin>:7:14: note: in instantiation of function template specialization 'g<double>' requested here</div>
<div> return x*g(x-1);</div><div> ^</div></div><div>[...]</div><div><br></div><div>This is not ideal, though. (There are a couple of other contexts where this kind of thing can happen, with constexpr and with decltype.) We should detect when we're recursively instantiating a function template specialization from within itself and bail out.</div>
</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Sep 19, 2013 at 1:34 AM, Yaron Keren <span dir="ltr"><<a href="mailto:yaron.keren@gmail.com" target="_blank">yaron.keren@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="rtl"><div dir="ltr">Hello,</div><div dir="ltr"><br></div><div dir="ltr">The following code causes endless recursion leading to seg fault with the latest SVN. </div>
<div dir="ltr"><br></div><div dir="ltr">clang tries to deduce g return type, which requires f return type, which requires g return type, repeats endlessly.</div>
<div dir="ltr"><br></div><div dir="ltr">If the templates are modified to regular functions taking double, clang correctly errors upon parse:</div><div dir="ltr">error: function 'g' with deduced return type cannot be used before it is defined</div>
<div dir="ltr"><div dir="ltr"> return x*g(x-1);</div></div><div dir="ltr"><br></div><div dir="ltr">but with templates there is no error parsing, just seg fault running.</div><div dir="ltr"><br></div><div dir="ltr">Yaron</div>
<div dir="ltr"><br></div><div dir="ltr"><br></div><div dir="ltr"><div dir="ltr"><font face="courier new, monospace">// Endless recursion with revision 190992.</font></div><div dir="ltr"><font face="courier new, monospace"><br>
</font></div><div dir="ltr"><font face="courier new, monospace">template <class T></font></div><div dir="ltr"><font face="courier new, monospace">auto g(T x);</font></div><div dir="ltr"><font face="courier new, monospace"><br>
</font></div><div dir="ltr"><font face="courier new, monospace">template <class T></font></div><div dir="ltr"><font face="courier new, monospace">auto f(T x) {</font></div><div dir="ltr"><font face="courier new, monospace"> if (x>0)</font></div>
<div dir="ltr"><font face="courier new, monospace"> return x*g(x-1);</font></div><div dir="ltr"><font face="courier new, monospace"> else</font></div><div dir="ltr"><font face="courier new, monospace"> return 1.0;</font></div>
<div dir="ltr"><font face="courier new, monospace">}</font></div><div dir="ltr"><font face="courier new, monospace"><br></font></div><div dir="ltr"><font face="courier new, monospace">template <class T></font></div>
<div dir="ltr"><font face="courier new, monospace">auto g(T x) {</font></div><div dir="ltr"><font face="courier new, monospace"> if (x>0)</font></div><div dir="ltr"><font face="courier new, monospace"> return x*f(x-1);</font></div>
<div dir="ltr"><font face="courier new, monospace"> else</font></div><div dir="ltr"><font face="courier new, monospace"> return 1.0;</font></div><div dir="ltr"><font face="courier new, monospace">}</font></div><div><font face="courier new, monospace"><br>
</font></div><div><font face="courier new, monospace">int main() {</font></div><div><font face="courier new, monospace"> f(9.0);</font></div><div><font face="courier new, monospace">}</font></div><div><br></div></div></div>
<br>_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div>