溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C++ 類模板、函數模板全特化、偏特化的使用

發布時間:2020-10-21 11:15:58 來源:腳本之家 閱讀:391 作者:jltxgcy 欄目:編程語言

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

#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 全特化版本函數

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女