[cfe-dev] Endless recursion due to return type deduction of mutually-recursive templates

Richard Smith richard at metafoo.co.uk
Thu Sep 19 22:18:44 PDT 2013


This is not endless recursion, just a stack overflow. I get:

<stdin>:2:6: fatal error: recursive template instantiation exceeded maximum
depth of 256
auto g(T x);
     ^
<stdin>:15:14: note: in instantiation of function template specialization
'f<double>' requested here
    return x*f(x-1);
             ^
<stdin>:7:14: note: in instantiation of function template specialization
'g<double>' requested here
    return x*g(x-1);
             ^
[...]

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.


On Thu, Sep 19, 2013 at 1:34 AM, Yaron Keren <yaron.keren at gmail.com> wrote:

> Hello,
>
> The following code causes endless recursion leading to seg fault with the
> latest SVN.
>
> clang tries to deduce g return type, which requires f return type, which
> requires g return type, repeats endlessly.
>
> If the templates are modified to regular functions taking double, clang
> correctly errors upon parse:
> error: function 'g' with deduced return type cannot be used before it is
> defined
>     return x*g(x-1);
>
> but with templates there is no error parsing, just seg fault running.
>
> Yaron
>
>
> // Endless recursion with revision 190992.
>
> template <class T>
> auto g(T x);
>
> template <class T>
> auto f(T x) {
>   if (x>0)
>     return x*g(x-1);
>   else
>     return 1.0;
> }
>
> template <class T>
> auto g(T x) {
>   if (x>0)
>     return x*f(x-1);
>   else
>     return 1.0;
> }
>
> int main() {
>   f(9.0);
> }
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130919/9c144088/attachment.html>


More information about the cfe-dev mailing list