MyClass.h:104: error: invalid explicit specialization before ‘>’ token
MyClass.h:104: error: enclosing class templates are not explicitly specialized
MyClass.h:105: error: template-id ‘method<0>’ for ‘void MyClass<Derived>::method()’ does not match any template declaration
Can I specialize with templates a method of a class? If yes, how?
Or is it impossible since partial specilized functions are not allowed ?
EDIT -- crap apparently this isn't the only problem. I just tried my fix and it isn't working either. Sorry about that.
Hold...
EDIT2 -- apparently you can't do a partial specialization like this. "enclosing class templates are not explicitly specialized
" seems to imply that the entire template (ie: the class) would need to be specialized as well. Which kind of makes sense when you think about it -- because if the class isn't specialized there's no guarantee that the funciton will be able to apply to the class. This seems like a strange use for a template anyway (why not just pass N as a parameter?)
<unrelated>
On a side note: this 'Derived' template class thing is throwing up a big red flag for me. Making a templated parent class that takes the child class as its template argument defeats the entire point of using inhertiance because the parent classes are no longer the same type. I don't know if this is what you're doing or not, but if it is you should really reconsider.
</unrelated>
This pattern is called the Curiously Recurring Template Pattern (CRTP) and is used quite commonly. For examples, see the boost::operator library, boost::spirit library, etc. Having said that, not all uses of CRTP
make sense, and it is impossible to know what OP is doing with the class.
The parenthesis are only a copy-paste error. I'll edit my previous post.
Ok, I'll explain wath is my goal: I'm constructing a QR decomposition for matrices. Each matrix has two compile-time variables (which are part of an enum) RowsAtCompileTime and ColsAtCompileTime. These variables can be either an int or the special value Dinamic. The scalar type is "saved" as typedef in the matrix class (MatrixType::Scalar is a typedef to a type, like e.g. float, double or complex<long double>). A class NumTraits stores some informations about the scalar in this way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* This code is part of Eigen (eigen.tuxfamily.org) */
template<typename T_> struct NumTraits<T_>{ };
template<> struct NumTraits<double>
{
typedefdouble Real;
typedefdouble FloatingPoint;
enum {
IsComplex = 0,
HasFloatingPoint = 1,
ReadCost = 1,
AddCost = 1,
MulCost = 1
};
};
In my QR class I store some informations at compile-time in this way:
This class has a (private) method compute_(), which actually does the decomposition, but this method has a different behaviour for dynamically-sized matrices. So, the constructor of QR must decide which method use, but since the decision must be taken at compile-time. My idea was:
1 2 3 4 5
QR::QR()
{
/* some "normal" code */
compute_<isDynamic>();
}
With two different specializations for compute_<0> and compute_<1>.