有没有办法让这个模板在 cpp 中工作?
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[]
,我会使用:
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)
{
}
相关文章:
- 有没有办法让这个模板在 cpp 中工作?
- 使用.cpp文件让 RcppArmadillo 在 Rstudio 中工作
- 创建链表,但无法按 CPP 的预期工作
- 仅一个功能的链接错误.在同一类和CPP文件中具有相似签名的其他函数工作正常
- 运算符重载在 CPP 中的排序中如何工作?
- 简单的 CPP 地图存储和访问无法按预期工作
- (c++)无法让 void 函数工作,主.cpp内部的几行也有问题
- CPP / JUCE 音频编程框架 - 滑块数组不起作用 - 单个滑块工作
- 程序编译和运行,但一段时间后,它停止使用Typedef关键字在CPP上工作
- CPP:超载的嵌套操作员无法正常工作
- "string::erase"如何在 cpp 中工作?
- 在macOSX上编译boost_asio/example/ssl/server.cpp可以在Linux上工作
- 不完整的参数错误,包括其他.cpp文件中的postgres.cpp工作文件
- 从工作iOS项目中导入.cpp文件并将其重命名为.mm问题
- CPP MQTT Broker代码不工作
- eof函数如何在cpp上工作
- 为什么在多个 cpp 文件中包含相同的标头,然后它们的编译工作
- CPP GDB 崩溃,没有核心和 GDB 附加到工作进程
- 相对于 cpp 不工作目录的路径
- 不确定查找功能在 CPP 中的工作原理