[cfe-dev] recursive template instantiation exceeded maximum depth of 1024

degski via cfe-dev cfe-dev at lists.llvm.org
Sun Jul 15 20:47:52 PDT 2018


Consider:

template<std::size_t N>
struct foobar : foobar<N - 1> { };

// some other condition for which this might get std::true_type.

template<>
struct foobar<0> : std::false_type { };

int main ( ) {

    std::cout << foobar<5000>::value << '\n';
}

This fails, unsurprisingly to anyone on this mailing list, with:

1>------ Build started: Project: project, Configuration: Debug x64 ------
1>main.cpp(334): fatal error : recursive template instantiation exceeded
maximum depth of 1024
1>main.cpp(334):  note: in instantiation of template class 'foobar<3976>'
requested here
1>main.cpp(334):  note: in instantiation of template class 'foobar<3977>'
requested here
1>main.cpp(334):  note: in instantiation of template class 'foobar<3978>'
requested here
1>main.cpp(334):  note: in instantiation of template class 'foobar<3979>'
requested here
1>main.cpp(334):  note: in instantiation of template class 'foobar<3980>'
requested here
1>main.cpp(334):  note: (skipping 1015 contexts in backtrace; use
-ftemplate-backtrace-limit=0 to see all)
1>main.cpp(334):  note: in instantiation of template class 'foobar<4996>'
requested here
1>main.cpp(334):  note: in instantiation of template class 'foobar<4997>'
requested here
1>main.cpp(334):  note: in instantiation of template class 'foobar<4998>'
requested here
1>main.cpp(334):  note: in instantiation of template class 'foobar<4999>'
requested here
1>main.cpp(343):  note: in instantiation of template class 'foobar<5000>'
requested here
1>main.cpp(334):  note: use -ftemplate-depth=N to increase recursive
template instantiation depth
1>Done building project "project.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

This state of affairs seems poor to me. I have a background in the prolog
programming language, in which recursive calls are ubiquitous, you cannot
live without, recursion all the way. In prolog, a similar bit of code would
be subject to tail-call optimization, which is a nice word for stating that
iff the last call of a predicate (a prolog procedure of sorts) is *the*
recursive one, that call will not be put on the stack (call frame re-use),
as there is nothing more to do, i.e. won't run out of stack-space (unless
there is no stop condition of course, then you have an endless loop).

Can this be implemented?

Have a nice day,

degski
-- 
*"If something cannot go on forever, it will stop" - Herbert Stein*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180716/17116bd2/attachment.html>


More information about the cfe-dev mailing list