有没有办法让这个模板在 cpp 中工作?

Is there any way to make this template in cpp work?

本文关键字:cpp 工作 有没有      更新时间:2023-10-16

完整代码在这里: 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可以替换任何数据类型,如chardoubleintfloat、...

但是,我想使用类似的东西:

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::arraystd::vector和其他可能的自定义矩阵类来重载operator[],我会使用:

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)
{
}