template<typename T> class CArray { static cosnt unsigned size = 10; T elems[size]; public: T& operator[](unsigned i) throw (std::out_of_range) { if (i >= size) { throw std::out_of_range("Access out of range\n"); } else { return elems[i]; } } };
1 template<typename T, unsigned Size> 2 class CArray2 3 { 4 T elems[Size]; 5 public: 6 T& operator[](unsigned i) throw (std::out_of_range) 7 { 8 if (i >= size) 9 { 10 throw std::out_of_range("Access out of range\n"); 11 } 12 else 13 { 14 return elems[i]; 15 } 16 } 17 };
template<typename T, unsigned Size> class CArray2 { public: CArray2() { id++; } ~CArray2(){} T elems[Size]; public: T& operator[](unsigned i) throw (std::out_of_range) { if (i >= size) { throw std::out_of_range("Access out of range\n"); } else { return elems[i]; } } public: static int id; }; template<typename T, unsigned Size> int CArray2<T, Size>::id = 0; //順便咱們也應該瞭解這種帶有非類型模板參數的模板類如何定義一個static成員 void main() { CArray2<char, 20> array0; printf("ID:%d\n", array0.id); CArray2<char, 20> array1; printf("ID:%d\n", array1.id); CArray2<int, 10> array3; printf("ID:%d\n", array3.id); CArray2<int, 20> array4; printf("ID:%d\n", array4.id); getchar(); }
1 template<typename T, void(*f)(T &v)> 2 void foreach(T array[], unsigned size) 3 { 4 for (unsigned i = 0; i < size; ++i) 5 { 6 f(array[i]); 7 } 8 } 9 10 template<typename T> 11 void inc(T &v){ ++v; } 12 13 template<typename T> 14 void dec(T &v){ --v; } 15 16 template<typename T> 17 void print(T &v){ printf("%d ", v); } 18 19 void main() 20 { 21 int array[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; 22 foreach<int, print<int>>(array, 8); 23 24 foreach<int, inc<int>>(array, 8); 25 26 getchar(); 27 }
1 template<int* p> 2 struct wrapper 3 { 4 int get(){ return *p; } 5 void set(int v){ *p = v; } 6 7 }; 8 9 template<int &p> 10 struct wrapper2 11 { 12 int get(){ return p; } 13 void set(int v){ p = v; } 14 }; 15 16 int global_variable = 0; 17 18 int main() 19 { 20 wrapper<&global_variable> gwrapper; 21 wrapper2<global_variable> gwrapper2; 22 }
1 template<int* p> 2 struct wrapper 3 { 4 public: 5 wrapper(){ id++; } 6 int get(){ return *p; } 7 void set(int v){ *p = v; } 8 public: 9 static int id; 10 }; 11 template<int* p> int wrapper<p>::id = 0; 12 13 int global_variable = 0; 14 int global_variable3 = 0; 15 16 int main() 17 { 18 wrapper<&global_variable> gwrapper; 19 printf("ID:%d\n", gwrapper.id); 20 21 wrapper<&global_variable> gwrapper4; 22 printf("ID:%d\n", gwrapper4.id); 23 24 wrapper<&global_variable3> gwrapper3; 25 printf("ID:%d\n", gwrapper3.id); 26 27 getchar(); 28 29 }
class some_value { int value; public: some_value(int _value) :value(_value){} int add_by(int op){ return value += op; } int sub_by(int op){ return value -= op; } int mul_by(int op){ return value *= op; } }; typedef int (some_value::* some_value_mfp)(int); template<some_value_mfp func> int call(some_value &value, int op){ return (value.*func)(op); }//*是必要的,不然會認爲是在使用value類的成員func void main() { some_value v0(0); printf("%d\n", call<&some_value::add_by>(v0, 1));//&是必要的,不然會認爲是調用some_value::add_by可是沒給參數 printf("%d\n", call<&some_value::sub_by>(v0, 2)); printf("%d\n", call<&some_value::mul_by>(v0, 3)); getchar(); }
template<typename T> struct inc { void operator()(T &v) const { ++v; } }; template<typename T> struct dec { void operator()(T &v) const { --v; } }; template<typename T> struct print { void operator()(T &v) const { std::cout << ' ' << v; } };
template<template<typename TT> class Func, typename T> void foreach(T array[], unsigned size) { Func<T> func; for (unsigned i = 0; i < size; i++) { func(array[i]); } }
void main() { int array[] = { 1, 2, 3, 4, 5, 6, 7 }; foreach<print>(array, 7); foreach<inc>(array, 7); foreach<dec>(array, 7); getchar(); }