# 模板的侷限性

``````template<class T>
bool compare(T& a, T& b) {
if (a > b) {
reture true}
else {
reture false;
}
}
int main() {
int a = 20; int b = 30;
bool jud = compare(a, b);
if (jud == true) {
cout << "a大" << endl;
}
if (jud == false) {
cout << "b大" << endl;
}
}
``````

``````class test {
public :
int number;
int age;
string rank;
};

bool compare(T& a, T& b) {
if (a > b) {
return true;
}
else {
return false;
}
}

int main() {
test a = { 10 };
test b = { 20 };

bool jud = compare(a, b);
if (jud == true) {
cout << "a大" << endl;
}
if (jud == false) {
cout << "b大" << endl;
}
}
``````

• 1.運算符重載（比較複雜所以這裏不太推薦）

• 2.具體化模板

``````class test {
public:
int number;
int age;
string rank;
};

template<class T>
bool compare(T& a, T& b) {
if (a > b) {
return true;
}
else {
return false;
}
}
template<> bool compare(test& a, test& b) {
if (a.age == b.age && a.number == b.number && a.rank == b.rank) {
return true;
}
else {
return false;
}
}
int main() {
test a = { 10,20 ,"一等" };
test b = { 20,20,"二等" };

bool jud = compare(a, b);
if (jud) {
cout << "a大" << endl;
}
if (!jud ) {
cout << "b大" << endl;
}
}
``````

``````template<> bool compare(test& a, test& b) {
if (a.age == b.age && a.number == b.number && a.rank == b.rank) {
return true;
}
else {
return false;
}
}
``````