herbertluo
herbertluo

Reputation: 79

Specializtion of function template with non-type template parameters

I used function template to calculate the determinant of a n dimension matrix.

template <int dimension>
double get_cofactor(double A[dimension][dimension], 
                    double temp[dimension-1][dimension-1], 
                    int p, int q)
{
    int i = 0, j = 0;
    for (int row = 0; row < dimension; row++)
    {
        for (int col = 0; col < dimension; col++)
        {
            if (row != p && col != q)
            {
                temp[i][j++] = A[row][col];

                if (j == dimension - 1)
                {
                    j = 0;
                    i++;
                }
            }
        }
    }
}

template <int dimension>
double determinant(double A[dimension][dimension])
{
    double ret = 0.0;

    double temp[dimension-1][dimension-1] ={ 0.0 };

    int sign = 1;
    for (int i=0; i<dimension; ++i) {
        get_cofactor<dimension>(A, temp, 0, i);
        ret += sign * A[0][i] * determinant<dimension-1>(temp);
        sign = -sign;
    } //end of for

    return ret;
}

I use the following specialization of function template to terminate the recursion.

template <>
double determinant<1>(double A[][1])
{
    return A[0][0];
}

But the specialization can not pass the compiling. One error occurred.

error C2912: explicit specialization 'double determinant<1>(double [][1])' is not a specialization of a function template

What's the problem? How can I terminate it?


2017/6/23 I modified the code, It works at GCC!

Upvotes: 0

Views: 59

Answers (0)

Related Questions