C++模板简单分析与举例
2013-04-10 14:23
330 查看
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
#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
相关文章推荐
- C++模板简单分析与举例
- C++模板简单分析与举例
- c++模板函数和模板类简单应用举例
- C++知识点之string,结合华为编程题:简单错误记录分析(附加容器pair知识点)
- C++模板举例
- throw()使用小结:More effective C++:审慎使用异常规格(转),简单举例
- 简单分析C++指针的操作和运算
- PHPCMS2008模板系统更新的简单分析
- C++动态绑定和模板的简单配合使用(virtual关键字)
- C++_模板举例_使用模板实现集合类(堆栈)
- 深入分析:C++模板究竟会使代码膨胀吗
- 分析模板的一段简单快速的算法片段
- C++模板实现的简单单链表
- C++ 模板应用举例_模板实现STL类(堆栈)
- c++ 链表(模板)简单实现
- 一个简单的c++模板开发实例
- 【data structure】线性表简单模板c++实现
- c++-模板不支持分离编译错误分析
- 【C++】单例模式的一种简单实现的模板
- Smarty模板常见的简单应用分析