<div dir="ltr"><div>Consider:</div><div><br></div><div>template<std::size_t N><br>struct foobar : foobar<N - 1> { };<br><br>// some other condition for which this might get std::true_type.<br><br>template<><br>struct foobar<0> : std::false_type { };<br><br>int main ( ) {<br><br>    std::cout << foobar<5000>::value << '\n';<br>}</div><div><br></div><div>This fails, unsurprisingly to anyone on this mailing list, with:</div><div><br></div><div>1>------ Build started: Project: project, Configuration: Debug x64 ------<br>1>main.cpp(334): fatal error : recursive template instantiation exceeded maximum depth of 1024<br>1>main.cpp(334):  note: in instantiation of template class 'foobar<3976>' requested here<br>1>main.cpp(334):  note: in instantiation of template class 'foobar<3977>' requested here<br>1>main.cpp(334):  note: in instantiation of template class 'foobar<3978>' requested here<br>1>main.cpp(334):  note: in instantiation of template class 'foobar<3979>' requested here<br>1>main.cpp(334):  note: in instantiation of template class 'foobar<3980>' requested here<br>1>main.cpp(334):  note: (skipping 1015 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all)<br>1>main.cpp(334):  note: in instantiation of template class 'foobar<4996>' requested here<br>1>main.cpp(334):  note: in instantiation of template class 'foobar<4997>' requested here<br>1>main.cpp(334):  note: in instantiation of template class 'foobar<4998>' requested here<br>1>main.cpp(334):  note: in instantiation of template class 'foobar<4999>' requested here<br>1>main.cpp(343):  note: in instantiation of template class 'foobar<5000>' requested here<br>1>main.cpp(334):  note: use -ftemplate-depth=N to increase recursive template instantiation depth<br>1>Done building project "project.vcxproj" -- FAILED.<br>========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========<br><br></div><div>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 <b>the</b> 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).</div><div><br></div><div>Can this be implemented?<br></div><div><br></div><div>Have a nice day,</div><div><br></div><div>degski<br></div><div>-- <br><div class="gmail_signature"><div dir="ltr"><i><b>"If something cannot go on forever, it will stop" - Herbert Stein</b></i><br></div></div>
</div></div>