1、使用自动类型推导时,必须要推导出一致的数据类型
2、必须要确定T的类型,
#include<iostream>
using namespace std;
template<class T>
void myswap(T& a, T& b) {
T tmp = a;
a = b;
b = tmp;
}
template<class T>
void func() {
cout << "func函数调用" << endl;
}
void test() {
int a = 10;
int b = 20;
char c = 'c';
//myswap(a, c);//推出的类型不一致
func<int>();//必须要确定T的类型,才可以使用
}
int main() {
test();
return 0;
}
才可以使用
文章探讨了在C++中使用模板类时,自动类型推导的要求,强调了推导出的数据类型必须一致。通过示例函数`myswap`和`func`,展示了在调用模板函数时需要明确指定类型的情况。
3555

被折叠的 条评论
为什么被折叠?



