编译器在乘以子类型时选择的运算符*错误

Wrong operator* chosen by compiler when multiplying subtypes

本文关键字:选择 运算符 错误 类型 编译器      更新时间:2023-10-16

我在自己的库中编写了一个通用矩阵类,其中包含像 +、-、* 这样的漂亮运算符。特别是,它有(函数体并不重要,所以你可以忽略它们,但我稍后仍然会引用它,所以很高兴有(:

template<typename T, int X, int Y>
Matrix<T,Y,X> operator*(const Matrix<T,Y,X> & left, const Matrix<T,Y,X> & up)
{
    Matrix<T,Y,X> result = Matrix<T,Y,X>::Zero;
    for (unsigned int j = 0; j<Y; j++)
        for (unsigned int i = 0; i<X; i++)
            for (unsigned int k = 0; k<X; k++)
                result[j][k] += left[j][i] * up[i][k];
    return result;
}
template<typename T, int Y, int X, typename U>
Matrix<T,Y,X> operator*(const Matrix<T,Y,X> & left, const U & right)
{
    // Expected to handle build-in types
    Matrix<T, Y, X> result = Matrix<T, Y, X>::Zero;
    for (int j = 0; j < Y; ++j)
        for (int i = 0; i < X; ++i)
            result[j][i] += left[j][i] * right;
    return result;
}

然后我写了Matrix4x4,一种专门用于旋转和平移等 3D 转换的Matrix子类型,因此它具有成员函数。当然,Matrix4x4是一个坏名字,我保证我会解决这个问题。

在使用Matrix4x4的代码中的某个时刻,我使用 operator*

// std::vector<Matrix4x4> mstackvertices({Matrix4x4::Identity});
mstackvertices.push_back(mstackvertices.back() * m_camera.m_projectionmatrix);

在这里,m_camera.m_projectionmatrix也是一个Matrix4x4

这应该调用第一个operator*,但属于第二个,因为 gcc 在以下行的第二个重载中给了我一个错误:

            result[j][i] += left[j][i] * right;

错误消息:

Matrix.hpp|169|error: no match for ‘operator*’ (operand types are ‘const float’ and ‘const swegl::Matrix4x4’)|
Matrix.hpp|169|note: candidates are:|
...

我的猜测是,Matrix4x4不完全Matrix,而只是一个子类型,但应用了一些规则,使 gcc 选择不涉及类型转换的最佳重载。

我不确定如何解决这个问题。我已经考虑了几种解决方案,但没有一个看起来很好:

  • 删除将接收内置类型的运算符,从而强制编译器选择唯一剩余的重载。这有效,但迫使我从一个看似完美运行的库中删除一个功能。
  • 使用组合而不是继承,并在Matrix方面重载所有相关Matrix4x4的运算符。
  • Matrix4x4中重新实现operator*。会有重复的代码,或者,如果我能设法调用带有强制转换的正确重载Matrix::operator*,那么,这仍然很麻烦。
  • 创建Matrix4x4::operator Matrix<float,4,4>() .它似乎不起作用,但我可能也在那里做错了什么。无论如何,我知道这会创建该对象的不受欢迎的副本。

这就是我现在所处的位置。还有其他想法吗?也许我一开始就做错了什么?我相信我会从中学到一些东西,所以非常欢迎任何帮助(:

编辑:

MatrixMatrix4x4的定义如下:

template<typename T, int Y, int X>
class Matrix
{
private:
    T data[Y][X];
    ...
};
class Matrix4x4 : public Matrix<float,4,4>
{
    ...
};

使用 Koenig 运算符,如下所示:

template<class T, int X, int Y>
class Matrix{
  // ...
public:
  // calculates the return value of `T*U`:
  template<class U>
  using R=decltype(std::declval<T const&>()*std::declval<U const&>());
  // maybe addin a `decay_t` to the above, if required.  (But who returns
  // a reference from binary `*`?)
  // Multiplication by a matrix on the RHS
  // The RHS dimension of this matrix, and the LHS dimension
  // of the RHS matrix, must match.  Accepts matrices with a
  // different underlying T.
  template<class U, int Z>
  Matrix<R<U>,X,Z> operator*(Matrix<U,Y,Z>const& rhs)const;
  // you can implement this operator here, or you can do it below
  // in the same header file.
  // This is the Koenig operator.  It is a friend operator that
  // is *not* a template, where the left hand side is a scalar of
  // type T, and the right hand side is our own type.
  friend Matrix<R<T>,X,Y> operator*(
    T const& lhs, Matrix const& rhs
  ){
    // implement here
  }
};
成员

矩阵比非成员更好地处理歧义。 friend 运算符就是我所说的 Koenig 运算符,必须在类中内联实现。 您可以调用另一个函数并异行实现该函数。

你也可以搞乱sfinae或标签调度,但以上是干净和简单的。 请注意,标量只允许在 lhs 上使用,因为Matrix * Scalar是......古怪。 Scalar * Matrix更传统。

正如 Yakk 所建议的那样,使用 decltype(Y*U) 作为返回类型可以删除类型不能相乘的重载,从而迫使编译器使用正确的版本。据我了解,这是SFINAE的用法:

template<typename T, int X, int Y, typename U, int Z>
Matrix<decltype(std::declval<T const&>()*std::declval<U const&>()),Y,X>
operator*(const Matrix<T,Y,X> & left, const Matrix<U,X,Z> & up)
{
    Matrix<T,Y,Z> result = Matrix<T, Y, Z>::Zero;
    for (unsigned int j = 0; j<Y; j++)
        for (unsigned int i = 0; i<X; i++)
            for (unsigned int k = 0; k<Z; k++)
                result[j][k] += left[j][i] * up[i][k];
    return result;
}
template<typename T, int Y, int X, typename U>
Matrix<decltype(std::declval<T const&>()*std::declval<U const&>()),Y,X>
operator*(const Matrix<T,Y,X> & left, const U & right)
{
    Matrix<T, Y, X> result = Matrix<T, Y, X>::Zero;
    for (int j = 0; j < Y; ++j)
        for (int i = 0; i < X; ++i)
            result[j][i] += left[j][i] * right;
    return result;
}

但是,某些运算符没有可以用这些术语声明的返回类型,例如operator+=。充其量它必须返回与左侧类型相同的类型,*this 。对于这些情况,enable_if可以按如下方式使用:

template<typename T, typename U, typename = decltype(std::declval<T const&>()+std::declval<U const&>())>
struct can_add
{
    static const bool value = true;
};
template<typename T, typename U>
struct can_add<T,U>
{
    static const bool value = false;
};
template<typename T, int Y, int X>
class Matrix
{
private:
    T data[Y][X];
public:
    // ...
    template<typename U, typename = std::enable_if<freon::can_add<T,U>::value>>
    void operator+=(const Matrix<U, Y, X> & other)
    {
        for (int j = 0; j < Y; ++j)
            for (int i = 0; i < X; ++i)
                data[j][i] += other[j][i];
    }
    template<typename U, typename = std::enable_if<freon::can_add<T,U>::value>>
    void operator+=(const U & other)
    {
        for (int j = 0; j < Y; ++j)
            for (int i = 0; i < X; ++i)
                data[j][i] += other;
    }
}