函数模板示例
template<typename T, int n>
T max(T (&arr)[n])
{
T maxm = arr[0];
for(int i = 1; i <n; ++i)
if (maxm < arr[i])
maxm = arr[i];
return maxm;
}
arr 也是像T 这样的类型参数吗?
最佳答案
arr
是函数参数的名称。它不是类型参数。它的类型是对元素类型 T
和长度 n
的数组的引用。
关于c++ - C++ 模板中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5529265/