Description of problem: Try to compile this file named ansi.cpp: with g++ ansi.cpp ----------------------------------------------------------------- template<int Depth, int A, typename B> struct K17 { static const int x = K17<Depth+1, 0, K17<Depth,A,B> >::x + K17<Depth+1, 1, K17<Depth,A,B> >::x + K17<Depth+1, 2, K17<Depth,A,B> >::x + K17<Depth+1, 3, K17<Depth,A,B> >::x + K17<Depth+1, 4, K17<Depth,A,B> >::x; }; template<int A, typename B> struct K17<16,A,B> { static const int x = 1; }; static const int z = K17<0,0,int>::x; ---------------------------------------------------------------- Version-Release number of selected component (if applicable): How reproducible: Steps to Reproduce: 1. g++ ansi.cpp 2. 3. Actual results: it hangs forever.. Expected results: it should compile Additional info:
If you monitor "top" whilst compiling you'll see that the process is gradually increasing in size. Something does, indeed, appear to be horked. $ g++ --version g++ (GCC) 3.3.1 20030915 (Red Hat Linux 3.3.1-5)
You're declaring approximately 5^16==152587890625 structs (actually exactly 5^16+5^15+5^14+...+5^2+5^1+1), how do you expect this to compile? Besides this is a typical example of what not to do with templates. Not to mention the fact that typename B and int A can both be removed without affecting the program. Oh, and even if it somehow managed to compile you'll still have the wrong value for z as it will overflow a 32-bit int. By the way your code is equivalent to static const int z = -2030932031;
The comment from maze above is entirely correct. The problem is not a bu g in the compiler or somewhere else but the fact that the problem gets to large to compile. For example by replacing the constant 16 by 3 the program compiles without a problem.
Indeed. Not a bug.