我有这个模板矩阵结构(我提供了一个接受std::initializer_list的构造函数):
template<int rows, int cols, typename scalar = float>
struct matrix;使用矩阵结构外部定义的乘积运算符,如下所示:
template<int n, int m, int p, typename scalar>
matrix<n, m, scalar> operator*(const matrix<m, p, scalar>& left, const matrix<p, n, scalar>& left);然后在结构中声明为朋友。所以如果我实例化两个矩阵:
matrix<2, 3> A = { 1, 2, 3, 4, 5, 6 };
matrix<3, 2> B = { 7, 8, 9, 10, 11, 12 };我想创建一个矩阵C=A* B,我必须这样写:
matrix<2, 2> C = A * B;这很好,但是有什么方法可以省略<2,2>模板吗?我相信它可以在编译时扣除(因为auto工作得很好):
auto C = A * B; // no errors我想只写matrix而不写auto,可以吗?
发布于 2015-09-01 21:03:27
不,你不能(如果你没有一些非模板基矩阵)。matrix不是一种类型,它是模板,您应该指定模板参数。auto是你能做的最简单的事情。或者,您可以使用decltype代替auto
decltype(A * B) C = A * B;https://stackoverflow.com/questions/32332337
复制相似问题