简体   繁体   English

编译时间模板值扣除

[英]Compile time template values deduction

I have this template matrix struct (I provided a constructor which takes std::initializer_list): 我有这个模板矩阵结构(我提供了一个使用std :: initializer_list的构造函数):

template<int rows, int cols, typename scalar = float>
struct matrix;

with a product operator defined outside the matrix struct, like this: 使用在矩阵结构外部定义的乘积运算符,如下所示:

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);

and then declared as a friend inside the struct. 然后在结构内声明为好友。 So if I instantiate two matrices: 因此,如果我实例化两个矩阵:

matrix<2, 3> A = { 1, 2, 3, 4, 5, 6 };
matrix<3, 2> B = { 7, 8, 9, 10, 11, 12 };

and I want to create a matrix C = A * B, I have to write: 我想创建一个矩阵C = A * B,我必须写:

matrix<2, 2> C = A * B;

And that's fine, but is there a way to omit the <2, 2> template? 很好,但是有没有办法省略<2,2>模板? I believe that it can be deducted at compile time (because auto works fine): 我相信可以在编译时扣除(因为自动工作正常):

auto C = A * B; // no errors

I'd like to write just matrix instead of auto , is it possible? 我想只写matrix而不是auto ,这可能吗?

No, you cannot (if you don't have some non-template base matrix). 不,您不能(如果您没有一些非模板基本矩阵)。 matrix is not a type, it's template and you should specify template parameters. matrix不是类型,而是模板,您应该指定模板参数。 auto is simplest thing, that you can do. auto是最简单的事情,您可以执行。 Or, instead of auto you can use decltype 或者,您可以使用decltype代替auto

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM