温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++类模板、函数模板全特化以及偏特化的实例用法

发布时间:2021-09-04 15:50:33 来源:亿速云 阅读:77 作者:chen 栏目:编程语言

这篇文章主要介绍“C++类模板、函数模板全特化以及偏特化的实例用法”,在日常操作中,相信很多人在C++类模板、函数模板全特化以及偏特化的实例用法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++类模板、函数模板全特化以及偏特化的实例用法”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

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

#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++类模板、函数模板全特化以及偏特化的实例用法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++
AI