template<typename T> class Add { public: T add(const T& a, const T& b) { T result = a + b; return result; } }; template<> class Add<std::string> //模板特化 { public: std::string add(const std::string& a, const std::string& b) { cout << "string add... "; std::string result = a +"__"+ b; return result; } }; int main() { Add<int> intAdd; int c = intAdd.add(1, 2); cout << c << endl; Add<std::string> strAdd; std::string cc =strAdd.add("hello", "world"); cout << cc << endl; system("pause"); }
特化模板