I'm trying to implement an matrix algorithm via template recursion, but I've run into a problem with my compiler. I don't know the right syntax to end the recursion. It doesn't quite fit into the simple pattern of ending the recursion with template specialization.

Here is a synopsis of the code:
Code:
template <int nrows, int ncols, int ld = nrows>
class cmatrix {

public:
	float *val ; // pointer to the data on the device
/* a bunch of code */
template <int uld>
inline void mgso( cupper<ncols,uld> &R)
{
/* some code */

if (ncols > 1) {
/* recurse over the remaining columns */

cmatrix<nrows,ncols-1,ld> X1(ptroff(0,1)) ;
/* ... */

/* recursion starts here */
X1.mgso(rsub) ;
}
}

} ;

/* cupper is a derived class from cmatrix */
template<int nrows, int ld = nrows>
class cupper : public cmatrix<nrows,nrows,ld> {
public:
	inline float *subptr()
	{
		return(val+1+ld) ;
	}

	cupper<nrows-1,ld> sub()
	{
		return(subptr()) ;

	}

/* other stuff like constructors and destructors etc. */

} ;
The problem is that the compiler ends up trying to constuct a cmatrix<nrows,0,ld>
where ncols=0, resulting in all kinds of errors. My compiler (MS visual studio 2005) gives me the error,

c:\projects\s3000\cudageo\cmatrix.h(93) : error C2057: expected constant expression
1> c:\projects\s3000\cudageo\cmatrix.h(518) : see reference to class template instantiation 'cmatrix<nrows,ncols,ld>' being compiled
1> with
1> [
1> nrows=5,
1> ncols=0,
1> ld=7
1> ]
Note that the recursion occurs with X1.mgso(rsub) ; It recurses despite the
if (ncols > 1) { ... } statement and it in fact tries to create X1 with ncols=0, despite the if statement.

The variable that recurses is not uld in the template definition, so direct template specialization is not quite right. Am I going to have create a special instance of cmatrix to get the recursion to stop?