标签:nbsp turn har template world each size 推断 程序设计
template<typename A,typenamen B,typename _C> A function(B arg){ _C var; ... }
即用<>中的替换template<>中的内容
#include <iostream> using namespace std; template<typename T> T add (T x, T y) { return x + y; } class Integer { public: Integer (int arg = 0) : m_var (arg) {} friend ostream& operator<< (ostream& os,Integer const& i) { return os << i.m_var; } Integer const operator+ (Integer const& rhs) const { return m_var + rhs.m_var; } private: int m_var; }; int main (void) { cout << add<int> (123, 456) << endl; cout << add<double> (1.3, 4.6) << endl; cout << add<string> ("Hello, ", "World !") << endl; // cout << add<char const*> ("Hello, ","World !") << endl; cout << add<Integer> (123, 456) << endl; return 0; }
#include <iostream> #include <typeinfo> using namespace std; template<typename T> void foo (T x, T y) { cout << "foo: " << typeid (x).name () << ‘ ‘ << typeid (y).name () << endl; } template<typename T> void bar (T const& x, T const& y) { cout << "bar: " << typeid (x).name () << ‘ ‘ << typeid (y).name () << endl; } template<typename R, typename T> R hum (T x) { R y; cout << "hum: " << typeid (x).name () << ‘ ‘ << typeid (y).name () << endl; return y; } void f1 (int x, int y) {} void f2 (double x, double y) {} int main (void) { int a, b; foo (a, b); // i i double c, d; bar (c, d); // d d char e[256], f[256]; foo (e, f); // Pc Pc bar (e, f); // A256_c A256_c // cout << sizeof (e) << endl; // 256 // char (*p)[256] = &e; // e[0] = ‘C‘; // *(e+0) = ‘C‘ foo ("hello", "tarena"); // PKc PKc // bar ("hello", "tarena"); // A6_c A7_c bar<string> ("hello", "tarena"); // Ss Ss bar (string ("hello"), string ("tarena")); f1 (a, c); // c: double -> int f2 (a, c); // a: int -> double // int i = 1.2; // cout << i << endl; // 隐式推断的同时不能隐式转换 // foo (a, c); // c: double -> int, T=int // a: int -> double, T=double foo ((double)a, c); // d d foo (a, (int)c); // i i foo<double> (a, c); // d d foo<int> (a, c); // i i // a = hum (c); // 返回值的类型不参与隐式推断 a = hum<int> (c); // d i return 0; }
#include <iostream> #include <typeinfo> #include <cstring> using namespace std; // 两个任意类型值的最大值 template<typename T> T const& max (T const& x, T const& y) { cout << "<1" << typeid (x).name () << ‘>‘ << flush; return x < y ? y : x; } // 两个任意类型指针所指向目标的最大值 template<typename T> T* const& max (T* const& x, T* const& y) { cout << "<2" << typeid (x).name () << ‘>‘ << flush; return *x < *y ? y : x; } // 两个字符指针所指向字符串的最大值 char const* const& max (char const* const& x,char const* const& y) { cout << "<3" << typeid (x).name () << ‘>‘ << flush; return strcmp (x, y) < 0 ? y : x; } /* char const* max (char const* x,char const* y){ cout << "<3" << typeid (x).name () << ‘>‘ << flush; return strcmp (x, y) < 0 ? y : x; } */ // 三个任意类型值的最大值 template<typename T> T const& max (T const& x, T const& y, T const& z) { cout << "<4" << typeid (x).name () << ‘>‘ << flush; return ::max (::max (x, y), z); } /* // 两个字符指针所指向字符串的最大值 char const* const& max (char const* const& x,char const* const& y) { cout << "<3" << typeid (x).name () << ‘>‘ << flush; return strcmp (x, y) < 0 ? y : x; } */ int main (void) { int a = 123, b = 456; cout << ::max (a, b) << endl; cout << *::max (&a, &b) << endl; char const* c = "ab", *d = "abc"; cout << ::max (c, d) << endl; cout << ::max<> (c, d) << endl; cout << ::max<char const*> (c, d) << endl; char const* e = "abcd"; char const* const& f = ::max (c, d, e); cout << f << endl; char const* g = "12", *h = "123",*i = "1234"; ::max (g, h, i); cout << f << endl; return 0; }
标签:nbsp turn har template world each size 推断 程序设计
原文地址:http://www.cnblogs.com/cotsnail/p/6158723.html