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)
source share