views:

80

answers:

3

I was just trying to learn the syntax of the beginner things, and how it worked when I was making this short bit of code in VS2008. The code below works in adding numbers 1 to 499, but if I add 1 to 500, the compiler bugs out giving me:

fatal error C1001: An internal error has occurred in the compiler.

And I was just wondering why that is. Is there some limit to how much code the compiler can generate or something and it just happened to be a nice round number of 500 for me?

#include <iostream>
using namespace std;

template < int b >
struct loop {
    enum { sum = loop< b - 1 >::sum + b };
};

template <>
struct loop< 0 > {
    enum { sum = 0 };
};

int main() {
    cout << "Adding the numbers from 1 to 499 = " << loop< 499 >::sum << endl;
    return 0;
}
+5  A: 

I assume with gcc (and by extension g++) the default maximum template recursion depth is 500 as at least on my machine I managed to reproduce your problems with a (slightly better) warning message. Compiling loop<500>::sum worked fine but trying to compile loop<501>::sum failed.

If you are using gcc (or g++) the solution is to compile it with -ftemplate-depth-## (Where ## is the maximum allowed depth).

So for example to compile main.cpp with a maximum template recursion depth of 2000

g++ -ftemplate-depth-2000 main.cpp

Or convert the code to this:

template < int b >
struct loop {
    enum { sum = (b*(b+1))/2 };
};

(But I will admit the above code doesn't help you learn about template metaprogramming)

Yacoby
Yeah I'm aware of that formula. Thanks though for the info.
Justen
+4  A: 

VC9 (VS2008) crashes with numbers > 499. The code itself is valid and compilers are even permitted to stop compilation after a certain amount of recursive instantiations, giving a diagnostic. However, an Internal Compiler Error (colloquially also called ICE) certainly isn't a nice diagnostic.

An ICE is always an error of the compiler. It might be caused by an error in the code, too, but if that's the case, then the compiler failed to show a proper diagnostic for that error. If the error is reproducible, you should submit a bug report to the compiler vendor, so they can fix their error.

When reporting such an error (here or elsewhere) you should never fail to provide the exact compiler version you used.

sbi
OP said that 1 to 499 works for him, but 1 to 500 does not.
Bill
@Bill: Damn, it seems I read too quickly. Sorry for that and thanks for pointing this out. I fixed my answer.
sbi
+2  A: 

Look in the Output window:

c:\projects\cpptemp3\cpptemp3.cpp(9):
fatal error C1001: An internal error has occurred in the compiler. (compiler file 'msc1.cpp', line 1411) To work around this problem, try simplifying or changing the program near the locations listed above.

Hans Passant
I wasn't looking for a way to circumvent the issue since this isn't really a program. I was try to understand why the issue was happening at 500 recursive iterations but not 400.
Justen
@Justen: You've reverse-engineered the template evaluational stack depth inside the compiler. 500, nice round number. Although I would have picked 512.
Hans Passant