编写重载模板
1 #include <iostream> 2 #include <sstream> 3 #include <string> 4 using namespace std; 5 6 template <typename T> 7 string debug_rep(const T &t) 8 { 9 ostringstream ret; 10 ret << t; 11 return ret.str(); 12 } 13 14 template <typename T> 15 string debug_rep(T *p) 16 { 17 ostringstream ret; 18 ret << "point: " << p; 19 if (p) 20 ret << " " << debug_rep(*p); 21 else 22 ret << " null pointer"; 23 24 return ret.str(); 25 } 26 27 int main() 28 { 29 string s("hi"); 30 cout << debug_rep(s) << endl; 31 cout << debug_rep(&s) << endl; 32 33 const string *sp = &s; 34 cout << debug_rep(sp) << endl; 35 return 0; 36 }
输出:
2.