Can’t use if constexpr

  algebra, c++, constexpr, metaprogramming, oop

My code below produces the following error:
C2131: expression did not evaluate to a constant.

    template<int32_t M, int32_t N>
    [[nodiscard]] constexpr double determinant(const Matrix<M,N> & m)
    {
        double det = 0;
        if constexpr(m.rows() == 2)
        {
            return m[0][0]*m[1][1] - m[0][1]*m[1][0];
        }
        else
        {
            for(std::size_t col = 0; col < m.cols(); ++col)
            {
                det += m[0][col] * cofactor(m, 0, col);
            }
        }
        return det;
    }

My "first" error without constepxr was: C1202: recursive type or function dependency context too complex.
Someone here on stackoverflow had a similar error while doing similar stuff, his solution was to use if constexpr because otherwise the if statement could not be evaluated at compile time. But this produces my error C2131.

Matrix<M,N> has a method rows() which is also constepxr. Can someone explain to me why this code won’t compile? Is it because the compiler needs to see the rows() implementation in the same header file? If so, is there another way to solve this error, maybe somehow forward declare the member function if such a thing is possible?

Source: Windows Questions C++

LEAVE A COMMENT