簡體   English   中英

編譯時間模板值扣除

[英]Compile time template values deduction

我有這個模板矩陣結構(我提供了一個使用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 C = A * B; // no errors

我想只寫matrix而不是auto ,這可能嗎?

不,您不能(如果您沒有一些非模板基本矩陣)。 matrix不是類型,而是模板,您應該指定模板參數。 auto是最簡單的事情,您可以執行。 或者,您可以使用decltype代替auto

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM