C ++ рекурсивная функция с шаблоном приводит к массиву нулевого размера - PullRequest
0 голосов
/ 27 января 2020

Я реализую детерминантную функцию в матричном классе, который я хотел бы вычислить путем рекурсивного получения все меньшей и меньшей вспомогательной матрицы. Проблема заключается в том, что компилятор в конечном итоге создает матрицу с размером 0, хотя у меня есть оператор if, который гарантирует, что этого не произойдет во время выполнения.

У меня есть два вопроса по этому поводу:

  1. Почему компилятор создает специализацию, которую нельзя создать во время выполнения? - У меня такое ощущение, что мне не хватает чего-то очень очевидного.
  2. Как мне изменить мой код, чтобы исправить рекурсию?

Вот заголовок со всем не связанные удалены. Полный исходный код здесь: https://github.com/DanielEverland/Artemis/blob/master/ArtemisEngineCore/Engine/Math/Matrices/GenericMatrix.h

Исходная матрица - матрица 4x4 типа double.


    template<class T, unsigned int rows, unsigned int columns>
    class GenericMatrix : BaseMatrix
    {
    public:
        // Returns the determinant of the matrix
        // Requires the matrix to be square
        T GetDeterminant() const
        {
            static_assert(rows == columns, "Cannot get determinant of non-square matrix");

            T determinant{};

            // Seeing as this is always false, a minor matrix is never created, and a zero-sized array should never be created.
            if(false)
                GetMinor(0, 0).GetDeterminant(); // If I comment-out this line the build succeeds

            return determinant;
        }

        // Returns the minor of this matrix.
        // Requires the matrix to be square.
        // Will return a matrix[N - 1, N - 1] with a removed row and column.
        GenericMatrix<T, rows - 1, columns - 1> GetMinor(unsigned int rowToDelete, unsigned int columnToDelete) const
        {
            static_assert(rows == columns, "Cannot get minor of non-square matrix");
            static_assert(rows >= 2, "Cannot get minor of a matrix that has 2 rows or fewer.");
            static_assert(columns >= 2, "Cannot get minor of a matrix that has 2 column or fewer.");

            GenericMatrix<T, rows - 1, columns - 1> minor{};

            unsigned int rowIndex = 0;
            for (unsigned int i = 0; i < minor.GetRows(); i++)
            {
                if (rowIndex == rowToDelete)
                    rowIndex++;

                unsigned int columnIndex = 0;
                for (unsigned int j = 0; j < minor.GetColumns(); j++)
                {
                    if (columnIndex == columnToDelete)
                        columnIndex++;


                    minor[i][j] = values[rowIndex][columnIndex];


                    columnIndex++;
                }

                rowIndex++;
            }

            return minor;
        }

    private:
        T values[rows][columns] = { };
    };

Вот выходные данные сборки. Как вы можете сказать, матрица с размером 0 создается

1>------ Build started: Project: UnitTests, Configuration: Debug x64 ------
1>MatrixTests.cpp
1>C:\Users\Daniel\source\repos\Artemis\ArtemisEngineCore\Engine\Math\Matrices\GenericMatrix.h(234,1): error C2087: 'values': missing subscript
1>C:\Users\Daniel\source\repos\Artemis\ArtemisEngineCore\Engine\Math\Matrices\GenericMatrix.h(200): message : see reference to class template instantiation 'ArtemisEngine::Math::Matrices::GenericMatrix<T,0,0>' being compiled
1>        with
1>        [
1>            T=Math::Matrices::T
1>        ]
1>C:\Users\Daniel\source\repos\Artemis\ArtemisEngineCore\Engine\Math\Matrices\GenericMatrix.h(194): message : while compiling class template member function 'T ArtemisEngine::Math::Matrices::GenericMatrix<T,1,1>::GetDeterminant(void) const'
1>        with
1>        [
1>            T=Math::Matrices::T
1>        ]
1>C:\Users\Daniel\source\repos\Artemis\ArtemisEngineCore\Engine\Math\Matrices\GenericMatrix.h(200): message : see reference to function template instantiation 'T ArtemisEngine::Math::Matrices::GenericMatrix<T,1,1>::GetDeterminant(void) const' being compiled
1>        with
1>        [
1>            T=Math::Matrices::T
1>        ]
1>C:\Users\Daniel\source\repos\Artemis\UnitTests\Math\MatrixTests.cpp(375): message : see reference to class template instantiation 'ArtemisEngine::Math::Matrices::GenericMatrix<Math::Matrices::T,1,1>' being compiled
1>C:\Users\Daniel\source\repos\Artemis\UnitTests\Math\MatrixTests.cpp(57): message : see reference to class template instantiation 'ArtemisEngine::Math::Matrices::GenericMatrix<Math::Matrices::T,4,4>' being compiled
1>C:\Users\Daniel\source\repos\Artemis\ArtemisEngineCore\Engine\Math\Matrices\GenericMatrix.h(234,1): warning C4200: nonstandard extension used: zero-sized array in struct/union
1>C:\Users\Daniel\source\repos\Artemis\ArtemisEngineCore\Engine\Math\Matrices\GenericMatrix.h(234,1): message : This member will be ignored by a defaulted constructor or copy/move assignment operator
1>Done building project "UnitTests.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 2 up-to-date, 0 skipped ==========

Build time 00:00:00.726
Build ended at 27/01/2020 12.56.52

1 Ответ

1 голос
/ 27 января 2020

Как уже указывалось в комментариях, вы должны остановить рекурсию, определив специализацию GenericMatrix. if(false) оценивается во время выполнения (как уже указывалось в комментариях, функция c ++ 17 if consexpr также может использоваться и здесь, однако следующий ответ не основан на этой функции, поскольку вопрос не помечен как c + +17). Это прямо отвечает на ваш первый вопрос. См. Следующий пример:

GenericMatrix<double,3,3> mat;
auto det = mat.GetDeterminant();

mat.GetDeterminant() внутренне вызывает GetMinor, который возвращает GenericMatrix<double,2,2>. Теперь сам возвращаемый GenericMatrix<double,2,2> -объект вызывает GetDeterminant, и все начинается с начала, поскольку if(false) не останавливает рекурсию времени компиляции. Следовательно, вы должны предоставить специализацию template<class T> GenericMatrix<T,1,1>, чья GetDeterminant не вызывает GetMinor

Что касается второго вопроса, вот упрощенный пример:

  #include <iostream> 

   template<class T, unsigned int rows, unsigned int columns>
    class GenericMatrix 
    {
    public:
        // Returns the determinant of the matrix
        // Requires the matrix to be square
        T GetDeterminant() const
        {
            T determinant = 0.;
            std::cout << "call GenericMatrix<T," << rows <<","
                      << columns <<">::GetDeterminant" << std::endl;
            auto det_minor = this->GetMinor(0, 0).GetDeterminant(); 
            // do semething with det_minor
            return determinant;
        }

    private:
        // Returns the minor of this matrix.
        // Requires the matrix to be square.
        // Will return a matrix[N - 1, N - 1] with a removed row and column.
        GenericMatrix<T, rows - 1, columns - 1> 
        GetMinor(unsigned int rowToDelete, unsigned int columnToDelete) const
        {
            GenericMatrix<T, rows - 1, columns - 1> minor{};
            std::cout << "call GenericMatrix<T," 
                      << rows <<","<< columns <<">::GetMinor with return type "
                      << "GenericMatrix<T," << rows-1 <<","
                      << columns-1 <<">::GetDeterminant" 
                      << std::endl;
            return minor;
        }

        T values[rows][columns] = { };
    };


    template<class T>
    class GenericMatrix<T,1,1> 
    {
    public:
        // Returns the determinant of the matrix
        T GetDeterminant() const
        {
            T determinant = 0.;
            std::cout << "call GenericMatrix<T,1,1>::GetDeterminant" << std::endl;
            return determinant;
        }

    private:
        T value = { };
    };


    int main()
    {
        GenericMatrix<double,4,4> mat;

        std::cout << mat.GetDeterminant() << std::endl;
        return 0;
    }

Вот скомпилированный код который выводит

call GenericMatrix<T,4,4>::GetDeterminant
call GenericMatrix<T,4,4>::GetMinor with return type GenericMatrix<T,3,3>::GetDeterminant
call GenericMatrix<T,3,3>::GetDeterminant
call GenericMatrix<T,3,3>::GetMinor with return type GenericMatrix<T,2,2>::GetDeterminant
call GenericMatrix<T,2,2>::GetDeterminant
call GenericMatrix<T,2,2>::GetMinor with return type GenericMatrix<T,1,1>::GetDeterminant
call GenericMatrix<T,1,1>::GetDeterminant
0
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...