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

Csaba Raduly via cfe-dev cfe-dev at lists.llvm.org
Mon Jul 16 01:06:28 PDT 2018


Hi degski,

You seem to be confusing run-time with compile-time. These errors are
issued by the compiler while analyzing the source. There is no call
stack yet.

If you want to compile this program, you can pass the
-ftemplate-depth=6000 option to the compiler.

On Mon, Jul 16, 2018 at 5:47 AM, degski via cfe-dev
<cfe-dev at lists.llvm.org> wrote:
> 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?
>



Csaba
-- 
You can get very substantial performance improvements
by not doing the right thing. - Scott Meyers, An Effective C++11/14 Sampler
So if you're looking for a completely portable, 100% standards-conformat way
to get the wrong information: this is what you want. - Scott Meyers (C++TDaWYK)


More information about the cfe-dev mailing list