编译器在乘以子类型时选择的运算符*错误
Wrong operator* chosen by compiler when multiplying subtypes
我在自己的库中编写了一个通用矩阵类,其中包含像 +、-、* 这样的漂亮运算符。特别是,它有(函数体并不重要,所以你可以忽略它们,但我稍后仍然会引用它,所以很高兴有(:
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>()
.它似乎不起作用,但我可能也在那里做错了什么。无论如何,我知道这会创建该对象的不受欢迎的副本。
这就是我现在所处的位置。还有其他想法吗?也许我一开始就做错了什么?我相信我会从中学到一些东西,所以非常欢迎任何帮助(:
编辑:
Matrix
和Matrix4x4
的定义如下:
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;
}
}
- 有条件地选择带有 decltype() 和三元运算符的类型
- 为什么选择转换运算符的重载?
- 是否可以在C++中使用三元运算符在 if 语句中选择比较运算符?
- 了解转换运算符的选择C++
- 为什么<iostream>运算符<<会选择明显错误的重载?
- 带大小参数和不带大小参数的"运算符删除":当两者都可用时,选择哪一个?
- 使用 std::function 时选择自动返回类型而不是构造函数的调用运算符
- 为什么在指针上对成员访问/元素选择有不同的运算符
- 强制编译器选择常量运算符重载
- 重载成员选择运算符
- 迭代器重载成员选择与间接运算符
- 关于智能指针中取消引用和成员选择运算符的定义
- clang-libc++错误:重载解析选择了隐式删除的复制赋值运算符
- C++ 编译器选择输出流运算符<<的错误重载
- 运算符 = 重载C++有选择地工作
- 转换是模棱两可的.标准隐式转换无法选择强制转换运算符
- 构造函数重载选择了强制转换运算符而不是结构类型
- iOS中箭头成员选择运算符的使用
- Visual C++ 使用成员选择运算符(. 或 –>)访问静态成员函数
- C++指针赋值和箭头成员选择运算符