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

Yaron Keren yaron.keren at gmail.com
Fri Sep 20 09:31:52 PDT 2013


Compiling under Visual C++ 2012 with the default stack = 1MB, the stack
overflows at depth of 216 instantiations, well before the 256 limit. Either
the maximum depth needs to be lower or the Visual C++ stack needs to be
larger using the /STACK:number_of_bytes linker parameter.

In any case, the "recursive template instantiation exceeded maximum depth"
error is followed by 256 notes where the instantiations happens...

Yaron



2013/9/20 Richard Smith <richard at metafoo.co.uk>

> 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/20130920/1f94b673/attachment.html>


More information about the cfe-dev mailing list