C++模板简单分析与举例

C++模板简单分析与举例

  #pragma once

  #include <iostream>

  /*/////////////////////////////////////////////

  C++ 模板

  /////////////////////////////////////////////*/

  /*

  --- 函数模板 ---

  */

  /// 声明

  template <typename T1, typename T2>

  void TFunc(T1, T2);

  /// 一般定义

  template <typename T1, typename T2>

  void TFunc(T1, T2)

  {

  std::cout << "一般" << std::endl;

  }

  /// 偏特化,T2(int)

  template <typename T1>

  void TFunc(T1, int)

  {

  std::cout << "偏特化" << std::endl;

  }

  /// 全特化,T1(char), T2(char)

  template <>

  void TFunc(char, char)

  {

  std::cout << "全特化" << std::endl;

  }

  /// 重载,函数的参数个数不同

  template <typename T1>

  void TFunc(T1)

  {

  std::cout << "重载" << std::endl;

  }

  /// 函数模板不允许默认的参数类型,而类模板允许

  // template <typename T1, typename T2=int>

  // void DefaultParamFun(T1, T2){}

  /// 测试模板函数

  void Test_Func()

  {

  TFunc<int,int>(0, 0); // 一般

  TFunc<char>('a', 0); // 偏特化

  TFunc('a','a'); // 全特化

  TFunc<int>(0); // 重载

  std::cout << std::endl;

  }

  /*

  --- 类模板 ---

  */

  /// 类模板允许默认的参数类型

  template <typename T1, typename T2=int>

  class TClass

  {

  public:

  TClass()

  {

  std::cout << "类模板,一般" << std::endl;

  }

  template <typename P>

  void Test(P)

  {

  std::cout << "类模板,模板成员函数,一般" << std::endl;

  }

  /// 特化成员函数 p(int)

  template <>

  void Test(int)

  {

  std::cout << "类模板,模板成员函数,偏特化" << std::endl;

  }

  static int m_nData;

  };

  /// 模板类静态成员初始化

  template<>

  int TClass<int,int>::m_nData = 0;

  /// 类模板偏特化 T2(int)

  template <typename T1>

  class TClass<T1, int>

  {

  public:

  TClass()

  {

  std::cout << "类模板,偏特化" << std::endl;

  }

  };

  /// 类的成员函数模板

  class TMemFun

  {

  public:

  template <typename T>

  void Test(T)

  {

  std::cout << "类的成员函数模板" << std::endl;

  }

  };

  /// 测试模板类

  void Test_Class()

  {

  TClass<bool,char> tClass1;

  tClass1.Test('a'); // 模板成员函数,一般

  tClass1.Test(0); // 模板成员函数,偏特化

  TClass<bool,int> tClass2; // 类偏特化

  TMemFun tMemFun;

  tMemFun.Test(1); // 类的成员函数模板

  std::cout << std::endl;

  }

  /*

  --- 类型自动转换 ---

  1、数组与指针互转

  2、限制修饰符const与非const互转

  */

  /// 参数为指针

  template <typename T>

  void PtrFun(T*){}

  /// 参数为数组

  template <typename T>

  void ArrayFun(T[]){}

  /// const参数

  template <typename T>

  void ConstFun(const T){}

  /// 非const参数

  template <typename T>

  void UnConstFun(T){}

  class CBase{}; // 父类

  class CDerived : public CBase{}; // 子类

  template <typename T>

  void ClassFun(T){}

  void Test_TypeConversion()

  {

  int nValue = 1;

  ConstFun(nValue); // 非const --> const

  const int cnValue = 1;

  UnConstFun(cnValue); // const --> 非const

  int nArray[2] = {0,0};

  PtrFun(nArray); // 数组 --> 指针

  int* pInt = NULL;

  ArrayFun(pInt); // 指针 --> 数组

  CDerived derived;

  ClassFun<CBase>(derived); // 子类 --> 父类

  // CBase base;

  // ClassFun<CDerived>(base); // 不允许,父类 --> 子类

  }

  void Test_Template()

  {

  Test_Func();

  Test_Class();

  Test_TypeConversion();

  }

转自:http://www.cdtarena.com/cpx/201304/8230.html

原文地址:https://www.cnblogs.com/cdtarena/p/3012310.html