Why do I get stack overflow?

Moritz Warning moritzwarning at web.de
Sun May 24 16:56:01 PDT 2009


On Sun, 24 May 2009 20:49:53 -0300, Ary Borenszweig wrote:

> When I compile this code I get "stack overflow" printed in the console.
> Anyone know why?
> 
> ---
> int fact(int X)() {
> 	if(X == 0) {
> 		return 1;
> 	} else {
> 		int temp = fact!(X - 1)();
> 		return X * temp;
> 	}
> }
> 
> const someVar = fact!(0)();
> ---

Because you generate fact!(-1)(), fact!(-2)() and so on at compile time.
You recursive template doesn't terminate.


More information about the Digitalmars-d-learn mailing list