D
Denis Remezov
Rob said:nsgi_2004 wrote in in
comp.lang.c++:
No a specialization is a completly new (and seperate) class,
here are three options:
1) Refactor your code in to a common base class say VectorBase:
template < typename T, int n >
class Vector : VectorBase< T, n >
{
// constructors and operator = () as required.
};
template <>
class Vector< float, 3 > : VectorBase< float, 3 >
{
// constructors and operator = () as required.
public:
Vector<float,3> cross_product(Vector<float, 3>& rhs)
{
return *this; // Cross product goes here.
}
};
2) Put the declaration of cross_product in Vector, but only
provide a defenition for the desired specialization:
template <>
Vector<float,3> Vector<float,3>::cross_product(Vector<float, 3>& rhs)
{
return *this;
}
Unfortunatly if this is called on an incorrect type you only get
a linker error, which are generaly rubbish (no line number etc).
3) Just implement cross_product in the (unspecialized) Vector.
4) Provide a partial specialisation of the class template for n=3
(using the definitions of #1):
template <typename T>
class Vector<T, 3> : public VectorBase<T, 3> {
public:
//...
const Vector cross_product(Vector const& rhs);
};
template <typename T>
const Vector<T, 3> Vector<T, 3>::cross_product(Vector const& rhs) {
return *this; // Cross product goes here.
}
Number 3 would be my default choice until I had found a good cause for a
specialisation (e.g. optimisation).
Denis