C++ 类模板、函数模板全特化、偏特化的使用

所属分类: 软件编程 / C 语言 阅读数: 46
收藏 0 赞 0 分享

一、类模板全特化、偏特化

#pragma once
#include <iostream>
#include <map>
 
template <typename T, typename U>
class TC
{
public:
 TC() 
 {
 std::cout << "泛化版本构造函数" << std::endl;
 }
 void funtest()
 {
 std::cout << "泛化版本成员函数" << std::endl;
 }
};
 
template<>
class TC<int, int>
{
public:
 TC()
 {
 std::cout << "全特化版本构造函数" << std::endl;
 }
 void funtest()
 {
 std::cout << "全特化版本成员函数" << std::endl;
 }
};
 
template<>
void TC<double, double>::funtest()
{
 std::cout << "全特化版本函数" << std::endl;
 
}

main.cpp

#include <iostream>
#include "template.h"
using namespace std;
 
int main()
{
 TC<char, int> tchar;
 tchar.funtest();
 TC<int, int> tint;
 tint.funtest();
 TC<double, double> tdouble;
 tdouble.funtest();
}

输出:

泛化版本构造函数
泛化版本成员函数
全特化版本构造函数
全特化版本成员函数
泛化版本构造函数
全特化版本函数

 二、类模板偏特化

1、模板参数数量上:

template.h

#pragma once
#include <iostream>
#include <map>
 
template <typename T, typename U, typename W>
class TC2
{
public:
 void funtest()
 {
 std::cout << "泛化版本成员函数" << std::endl;
 }
};
 
template <typename U>
class TC2<int, U, double>
{
public:
 void funtest()
 {
 std::cout << "偏特化版本成员函数" << std::endl;
 }
};

main.cpp

#include <iostream>
#include "template.h"
using namespace std;
 
int main()
{
 TC2<double, double, double> tdouble2;
 tdouble2.funtest();
 TC2<int, double, double> tint2;
 tint2.funtest()
}

输出:

泛化版本成员函数
偏特化版本成员函数

2、从模板参数范围:

template.h

#pragma once
#include <iostream>
#include <map>
 
template <typename T>
class TC3
{
public:
 void funtest()
 {
 std::cout << "泛化版本成员函数" << std::endl;
 }
};
 
template <typename T>
class TC3<const T>
{
public:
 void funtest()
 {
 std::cout << "const T偏特化版本成员函数" << std::endl;
 }
};
 
template <typename T>
class TC3<T&>
{
public:
 void funtest()
 {
 std::cout << "T&偏特化版本成员函数" << std::endl;
 }
};
 
template <typename T>
class TC3<T *>
{
public:
 void funtest()
 {
 std::cout << "T *偏特化版本成员函数" << std::endl;
 }
};

main.cpp

#include <iostream>
#include "template.h"
using namespace std;
 
int main()
{
 TC3<int> tint3;
 tint3.funtest();
 TC3<int &> tint3_ref;
 tint3_ref.funtest();
 TC3<int *> tint3_point;
 tint3_point.funtest();
 TC3<const int> tint3_const;
 tint3_const.funtest();
}

输出:

泛化版本成员函数
T&偏特化版本成员函数
T *偏特化版本成员函数
const T偏特化版本成员函数

 三、函数模板全特化(不能偏特化)

template.h

#pragma once
#include <iostream>
#include <map>
 
template <typename T, typename U>
void tfunc(T& a, U& b)
{
 std::cout << "tfunc 泛化版本函数" << std::endl;
}
 
template <>
void tfunc(int& a, int& b)
{
 std::cout << "tfunc 全特化版本函数" << std::endl;
}

main.cpp

#include <iostream>
#include "template.h"
using namespace std;
 
int main()
{
 int a1 = 1;
 double b1 = 3.2;
 tfunc(a1, b1);
 tfunc(a1, a1);
}

输出:

tfunc 泛化版本函数
tfunc 全特化版本函数

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

C语言数组入门之数组的声明与二维数组的模拟

这篇文章主要介绍了C语言数组入门之数组的声明与二维数组的模拟,数组学习的同时也要相应理解C语言指针的作用,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言中变量与其内存地址对应的入门知识简单讲解

这篇文章主要介绍了C语言中变量与其内存地址对应的入门知识简单讲解,同时这也是掌握指针部分知识的基础,需要的朋友可以参考下
收藏 0 赞 0 分享

讲解C语言编程中指针赋值的入门实例

这篇文章主要介绍了讲解C语言编程中指针赋值的入门实例,通过const int i与int *const pi这样两个例子来分析指针的赋值和地址指向,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言中的结构体的入门学习教程

这篇文章主要介绍了C语言中的结构体的入门学习教程,以struct语句定义的结构体是C语言编程中的重要基础,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言编程入门之程序头文件的简要解析

这篇文章主要介绍了C语言编程入门之程序头文件的简要解析,包括头文件重复包含问题等方面的说明,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言编程中的联合体union入门学习教程

这篇文章主要介绍了C语言编程中的联合体union入门学习教程,也是C语言入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

C语言中数组作为函数的参数以及返回值的使用简单入门

这篇文章主要介绍了C语言中数组作为函数的参数以及返回值的使用简单入门,这里以一维数组作为基本条件进行例子讲解,需要的朋友可以参考下
收藏 0 赞 0 分享

MySQL的内存表的基础学习教程

这篇文章主要介绍了MySQL的内存表的基础学习教程,包括内存表的创建以及使用限制等等,需要的朋友可以参考下
收藏 0 赞 0 分享

C++中头文件的概念与基本编写方法

这篇文章主要介绍了C++中头文件的概念与基本编写方法,是C++入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

jQuery移动页面开发中主题按钮的设计示例

这篇文章主要介绍了jQuery移动页面开发中主题按钮的设计示例,jQuery是当今最具人气的JavaScript开发类库,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多