0

I have a template class defined as follow :

template<unsigned int N, unsigned int L>
class Matrix
{
    public:

        Matrix(const float k = 0.0f)
        {
            for(unsigned int i = 0; i < N; ++i)
                for(unsigned int j = 0; j < L; ++j)
                {
                    if(i == j) m[i][j] = k;
                    else m[i][j] = 0.0f;
                }
        }

        //With other constuctors, methods and operators

    protected:

        float m[N][L];
};

I want to make a specialization for Matrix<4u, 4u>
All the functions will do the same thing that the ones from the template except one that I will override:

Vec3 operator*(const Vec3& vec)
{
    Vec3 v;

    if(L != 3)
    {
        if(Core::Debug::Log::CoreLogIsActive)
            Core::Debug::Log::ConsoleLog("Number of column of the matrix is different from the number of column of the vector",
                Core::Debug::LogLevel::Error);
        return v;
    }

    v.x = m[0][0] * vec[0] + m[0][1] * vec[1] + m[0][2] * vec[2];
    v.y = m[1][0] * vec[0] + m[1][1] * vec[1] + m[1][2] * vec[2];
    v.z = m[2][0] * vec[0] + m[2][1] * vec[1] + m[2][2] * vec[2];

    return v;
}

(It will do a different stuff)

How should I make the specilization ?
The other functions doesn't change from my specilization Matrix<4u, 4u> but if I make a class specilization I will have to specilize every member won't I ?

My guess would be to specilize only the member function that have to, then just use

using Matrix4 = Matrix<4u, 4u>

Would that be enough ?
And will I be able in the specilization to use an operator from the Matrix<4u, 4u> like :

template <>
Vec3 Matrix<4u, 4u>::operator*(const Vec3& vec)
{
    Vec4 vec4d(vec);

    vec4d = Matrix<4u, 4u>::operator*(vec);

    return Vec3(vec4d.x, vec4d.y, vec4d.z);
}

Also, I intend to then use inheritance with Matrix4

Thanks for your time

e.farman
  • 335
  • 2
  • 12

1 Answers1

0

Well, what I said seems to work (I had typos that I couldn't find ...)

Solution :

template <>
Vec3 Matrix<4u, 4u>::operator*(const Vec3& vec)
{
    Vec4 vec4d(vec);

    vec4d = Matrix<4u, 4u>::operator*(vec);

    return Vec3(vec4d.x, vec4d.y, vec4d.z);
}

using Matrix4 = Matrix<4u, 4u>
e.farman
  • 335
  • 2
  • 12