Just introducing yourself as TMPing and stumbling into a quirk

I was just trying to learn the newbie syntax and how it worked when I was doing this short bit of code in VS2008. The code below works with adding numbers from 1 to 499, but if I add 1 to 500, the compiler will disable me:

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

And I'm just wondering why that is. Is there any limit to how much code the compiler or something like that can generate, and for me that was a nice round number of 500?

 #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; } 
+4
source share
3 answers

I assume that with gcc (and with the g ++ extension) the maximum recursion depth is 500 by default, since at least on my machine I was able 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 should compile it with -ftemplate-depth-## (where ## is the maximum allowed depth).

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

 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 agree that the code above will not help you learn about metaprogramming templates)

+5
source

VC9 (VS2008) crashes with numbers> 499. The code itself is valid, and compilers are even allowed to stop compiling after a certain number of recursive instances, giving diagnostics. However, an internal compiler error (also called ICE in a conversation), of course, is not a good diagnosis.

ICE is always a compiler error. It may also be caused by an error in the code, but if this happens, the compiler could not show the correct diagnosis of this error. If the error is reproducible, you should send an error report to the compiler provider so that they can fix their error.

When presenting such an error (here or elsewhere), you should never refuse to provide the exact version of the compiler that you used.

+5
source

Look in the output window:

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

+2
source

Source: https://habr.com/ru/post/1306935/


All Articles