编译时间模板值扣除

时间:2015-09-01 12:59:27

标签: c++ templates matrix compile-time

我有这个模板矩阵结构(我提供了一个带有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);

然后在struct中声明为朋友。所以,如果我实例化两个矩阵:

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;

那没关系,但有没有办法省略&lt; 2,2&gt;模板?我相信它可以在编译时扣除(因为自动工作正常):

auto C = A * B; // no errors

我只想写matrix代替auto,是否有可能?

1 个答案:

答案 0 :(得分:3)

不,你不能(如果你没有一些非模板基矩阵)。 matrix不是类型,它是模板,您应该指定模板参数。 auto是最简单的事情,你可以做到。或者,您可以使用auto

代替decltype
decltype(A * B) C = A * B;