[英]Is there any way to make this template in cpp work?
完整代碼在這里: https : //pastebin.com/XXutDDjP
整個項目在這里發布可能會有點混亂,但我在以下部分感到困惑。
看看這個代碼:
template <typename T>
T myMax(T x, T y) {
return (x > y) ? x : y;
}
例如,此代碼匹配參數,
myMax(int x, int y)
, myMax(char x, char y)
, ...
因此, T
可以替換任何數據類型,如char
、 double
、 int
、 float
……
但是,我想使用類似的東西:
template <typename T>
int myMax(int A[T][T], int n) {
// ...
}
因此它可以用於A[T][T]
、 myMax(A[5][5], 2)
、 myMax(A[7][7], 5)
、...
為了允許使用 C 樣式數組、 std::array
、 std::vector
和其他可能重載operator[]
自定義 Matrix 類,我將使用:
template <typename T>
int myMax(T const& matrix, int n) {
// ...
}
只需將typename
更改為size_t
:
template<size_t T>
int myMax(int A[T][T], int n)
{
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.