Consider the following contrived example of defining an array template:
template <typename t, unsigned int n> class TBase
{
protected:
t m_Data[n];
};
template <typename t, unsigned int n> class TDerived : public TBase<t, n>
{
TDerived()
{
}
};
I can specialize this type to provide a non-default constructor for an array of length 2 as follows:
template <typename t> class TDerived<t, 2> : public TBase<t, 2>
{
public:
TDerived(const t& x0, const t& x1)
{
m_Data[0] = x0;
m_Data[1] = x1;
}
};
int main()
{
TDerived<float, 2> Array2D_A(2.0f, 3.0f);
TDerived<float, 3> Array3D_A;
return 0;
}
Is there any other way to create a class that has different constructor parameters, limited by template parameters at compile time, without requiring full class specialization for each variant?
In other words, is there any way that I can have specialized constructors in a class TBasewithout the need for an intermediate stage of creation TDerivedwhile maintaining functionality TBase?
Munro