#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
//Template function prototype
template<class T> T plus(const T& a, const T& b);
string plus(const string& a, const string& b);
void main() {
cout << "plus(3, 4) = " << plus(3, 4) << endl;
cout << "plus(3.2, 4.2) = " << plus(3.2, 4.2) << endl;
cout << "plus(\"he\", \"llo\") = " << plus(static_cast<string>("he"), static_cast<string>("llo")) << endl;
cout << "plus(static_cast<double>(3), 4.2) = " << plus(static_cast<double>(3), 4.2) << endl;
cout << "plus(3, static_cast<int>(4.2)) = " << plus(3, static_cast<int>(4.2)) << endl;
}
template<class T> T plus(const T& a, const T& b) {
return a + b;
}
string plus(const string& a, const string& b) {
cout << "specifica" << endl;
return a + b;
}
运行结果:
plus(3, 4) = 7
plus(3.2, 4.2) = 7.4
specifica
plus("he", "llo") = hello
plus(static_cast<double>(3), 4.2) = 7.2
plus(3, static_cast<int>(4.2)) = 7