当前位置:首页 > 通信资讯 > 正文

C++模板特化与偏特化(c++ 模板 特化)

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

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #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

?
1 2 3 4 5 6 7 8 9 10 11 12 13 #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

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #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

?
1 2 3 4 5 6 7 8 9 10 11 #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

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #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

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #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

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #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

?
1 2 3 4 5 6 7 8 9 10 11 #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 全特化版本函数

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

原文链接:https://blog.csdn.net/jltxgcy/article/details/104201783

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。