Just introducing myself to TMPing, and came across a quirk

Posted by Justen on Stack Overflow See other posts from Stack Overflow or by Justen
Published on 2010-04-14T14:55:33Z Indexed on 2010/04/14 15:23 UTC
Read the original article Hit count: 400

Filed under:
|
|

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. 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;
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates