mclaurentiu

Consider this example:

 

template <int N>
struct Factorial
{
    enum { value = N * Factorial<N - 1>::value };
};

template <>
struct Factorial<0>
{
    enum { value = 1 };
};


void foo()
{
    int x = Factorial<4>::value;

}

 

It compiles and runs fine. But if you remove the code in bold, the C++ compiler crashes (probably because of a stack overflow).

I tried looking for a support link in order to report this but found none.

 

P.S.: code sample taken from http://en.wikipedia.org/wiki/Template_metaprogramming



Re: Visual C++ Language Compile-time class generation causes C++ Compiler to Crash

Mike Danes

Without that specialization of Factorial the result will be a infinite recursion so an error is not unexpected (other compilers will give an error here).

Of course an error message (like "template depth exceeded" or something) would be better than a compiler crash so feel free to report this at http://connect.microsoft.com/VisualStudio





Re: Visual C++ Language Compile-time class generation causes C++ Compiler to Crash

mclaurentiu

Even with the specialization the compiler will probably crash when a large number is specified, like: int x = Factorial<496>::value; (since the compiler crashes after generating the template for Factorial<-490>).

Thank you for the link, I will report the error.





Re: Visual C++ Language Compile-time class generation causes C++ Compiler to Crash

Mike Danes

"Even with the specialization the compiler will probably crash when a large number is specified"

Indeed, there is a limit for nested template instantiations. ISO specifies a minimum of 17, Visual C++ appears to have a 490 limit and g++ 500.





Re: Visual C++ Language Compile-time class generation causes C++ Compiler to Crash

mclaurentiu

Mike Danes wrote:

Indeed, there is a limit for nested template instantiations. ISO specifies a minimum of 17, Visual C++ appears to have a 490 limit and g++ 500.

Visual C++ appears to have a 495 limit actually :)

(Factorial<4>,Factorial<3>,Factorial<2>,Factorial<1>,Factorial<0> plus another 490).





Re: Visual C++ Language Compile-time class generation causes C++ Compiler to Crash

Mike Danes

Don't know, I tried Factorial<489> and Factorial<490>. 489 works and 490 does not so I assumed 490



Re: Visual C++ Language Compile-time class generation causes C++ Compiler to Crash

mclaurentiu

That's weird, 495 works fine for me, 496 does not



Re: Visual C++ Language Compile-time class generation causes C++ Compiler to Crash

Marius Bancila

I once tried a depth of 1,000,000. Wink Obviously the compiler crashed...

http://programare.org/viewtopic.php p=7542&highlight=#7542