I am writing some kind of sparse matrix implementation, in fact there are two different implementations: one for light types (i.e. sizeof(T) <= sizeof(int64) and one for heavy types.
Depending on sizeof(T) , I want to instantiate the corresponding class. At first, I tested a superclass that creates an implementation of HeavyType or LightType, but this requires both light and heavy inherit from the common virtual BaseClass, and the general call class uses this or that (not very clean) one in this way:
template <class T> class Generic{ public: Generic(){ if (sizeof(T) > TRESHOLDVALUE) matrix_ = new HeavyType<T>(); else matrix_ = new LightType<T>(); } private: matrix_ * BaseClass<T>; };
This works, but it's not clean, and virtualization in BaseClass slows down execution ...
I would like to write only one class of templates and specialize it for several types, but I wonder: is it possible to specialize in a specific value of sizeof(T) (i.e. equivalent to if (sizeof(T) <= sizeof(int64)) )? or for an array of possible types ( template <> class Matrix<arrayOfPossibleTypes> )?
I would like to avoid re-writing the class for int , bool , uint_32 , int32 , etc types.
Does anyone have any ideas?
PS: As an alternative, I thought the pre-compiler macro selects the LightType or HeavyType class, but I think it's impossible to use sizeof() in the #if ancestor compiler.
source share