|
-
January 1st, 2009, 11:23 PM
#1
[RESOLVED] template partial specialization
Hello,
I am trying to implement a dense and sparse matrix class using template specialization. The first two class method definitions work (one with no specialization, one with full specialization), but the third definition with partial specialization does not: if the commented region is uncommented, the example fails to compile.
Code:
#include <iostream>
using std::cout;
enum MatrixType{DENSE,SPARSE};
template<class T,MatrixType MT>
struct Matrix{ Matrix(); };
template<class T, MatrixType MT>
Matrix<T,MT>::Matrix<T,MT>(){
cout << "default constructor\n";
}
template<>
Matrix<double,DENSE>::Matrix<double,DENSE>(){
cout << "<double,DENSE> constructor\n";
}
/*
template<class T>
Matrix<T,DENSE>::Matrix<T,DENSE>(){
cout << "<T,DENSE> constructor\n";
}
*/
int main(){
Matrix<double,SPARSE> V1;
Matrix<int,DENSE> V2;
Matrix<double,DENSE> V3;
return 0;
}
One fix is to do full specializations of all needed types, but this requires a lot of code duplication. I suspect that this has to do with Matrix<T,DENSE> not being recognized as a valid class. Any ideas how to efficiently implement this, or something structurally similar?
Last edited by jakevdp; January 1st, 2009 at 11:31 PM.
Reason: added code tag
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|