我对C++模板的实例化方式感到困惑。我有一段代码:
template <class T, int arraySize>
void test1(T (&array)[arraySize])
{
cout << typeid(T).name() << endl;
}
template<class T>
void test2(T &array)
{
cout << typeid(T).name() << endl;
}
int main()
{
int abc[5];
test1(abc);
test2(abc);
return 0;
}
以下是我的问题:
1. 数组 abc 的大小如何传递给 test1(参数 arraySize)?
2. C++ 编译器如何确定两个模板中的 T 的类型?
test1<int, 5>(abc)
吗?第二个根本没有意义。你有一个函数test2<T>
,你却像访问数组一样对它进行索引操作?! - Shahbaz