template instantiation depth

Hi everyone,

im currently writing a template matrix class, and i got the following error message when compiling my code:

error: template instantiation depth exceeds maximum of 500 instantiating ‘D3::Matrix<type, 4u>::Matrix(type) [with type = long double]’,
possibly from virtual table generation (use -ftemplate-depth-NN to increase the maximum)


What causes the error are the constructors, operator=, and other methods which uses instances of Matrix class with a modified dim parameter (in my case dim-1 is causing the compiler to recursively create instances of my Matrix class)

That's a shortened version of the class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template<typename type, unsigned int dim>
class Matrix {
	private:
		type mat[dim*dim];
	public:
		Matrix(const Matrix<type, dim>& cp);
		Matrix(const Matrix<type, dim-1>& cp);
		Matrix(const Matrix<type, dim+1>& cp);

		void adjoint(Matrix<type, dim>& result);

		const Matrix<type, dim>& operator=(const Matrix<type, dim-1>& rval);
};

//Implementation of void adjoint():
template<typename type, unsigned int dim>
void Matrix<type, dim>::adjoint(Matrix<type, dim>& result) {
	Matrix<type, dim-1> subMat;

	//[...]
}


So basically i want to be able to easily construct a matrix of another matrix that's either one dimension smaller or larger (by removing last row/column or adding one "0 0 0 0...1" row/column) and i want to be able to use matrices of a lower dimension like shown in the void adjoint method.

Is there any way to prevent the compiler from recursively creating 'infinite' instances of the class? Would be great if it's possible to drop some of the classes methods in case the compiler created the class recursively. If i'd be able to drop all of the methods that make use of "Matrix<type, dim-1>" in case the instance was created recursively, the compiler would not create another instance, preventing this error.

Is there any way to do this or are there other solutions to my problem?
Last edited on
Question: Why use templates to create matrices? What's the point?
Because like that i have more control about the classes methods. Im using various specializations, i.e. for dim=3 and dim=4

for dim=4 it makes sense to have a method which creates an orthographic projection matrix, for any other dimension it makes no sense.

for dim=3 and dim=4 it makes sense to have a method that creates a rotation matrix, but it doesnt make sense for other dimensions.

I prefer to get error messages during compilation, not during runtime of the program, that's the reason
What you are saying is that you want to create a template that allows you to create matrices with incompatible behavior. I don't see how that's supposed to be useful. Why not write a generic matrix class that creates a [n][m] matrix, and create special classes for special matrices that use this matrix class? That achieves the same thing, is much less complicated and most importantly, doesn't obfuscate what you are actually trying to do.
compile time assert http://www.boost.org/doc/libs/1_47_0/doc/html/boost_staticassert.html
1
2
3
template<size_t dim2>
const Matrix<type, dim>& operator=(const Matrix<type, dim2>& rval){
  BOOST_STATIC_ASSERT(dim==dim2 or dim==dim2+1 or dim==dim2-1);
(didn't test it)

However it may be better to make them different classes (instead of templates).
Topic archived. No new replies allowed.