C++類模板、函數(shù)模板全特化、偏特化的使用

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

創(chuàng)新互聯(lián)公司為您提適合企業(yè)的網(wǎng)站設計?讓您的網(wǎng)站在搜索引擎具有高度排名,讓您的網(wǎng)站具備超強的網(wǎng)絡競爭力!結(jié)合企業(yè)自身,進行網(wǎng)站設計及把握,最后結(jié)合企業(yè)文化和具體宗旨等,才能創(chuàng)作出一份性化解決方案。從網(wǎng)站策劃到成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設, 我們的網(wǎng)頁設計師為您提供的解決方案。

#pragma once
#include <iostream>
#include <map>
 
template <typename T, typename U>
class TC
{
public:
 TC() 
 {
 std::cout << "泛化版本構造函數(shù)" << std::endl;
 }
 void funtest()
 {
 std::cout << "泛化版本成員函數(shù)" << std::endl;
 }
};
 
template<>
class TC<int, int>
{
public:
 TC()
 {
 std::cout << "全特化版本構造函數(shù)" << std::endl;
 }
 void funtest()
 {
 std::cout << "全特化版本成員函數(shù)" << std::endl;
 }
};
 
template<>
void TC<double, double>::funtest()
{
 std::cout << "全特化版本函數(shù)" << 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();
}

輸出:

泛化版本構造函數(shù)
泛化版本成員函數(shù)
全特化版本構造函數(shù)
全特化版本成員函數(shù)
泛化版本構造函數(shù)
全特化版本函數(shù)

 二、類模板偏特化

1、模板參數(shù)數(shù)量上:

template.h

#pragma once
#include <iostream>
#include <map>
 
template <typename T, typename U, typename W>
class TC2
{
public:
 void funtest()
 {
 std::cout << "泛化版本成員函數(shù)" << std::endl;
 }
};
 
template <typename U>
class TC2<int, U, double>
{
public:
 void funtest()
 {
 std::cout << "偏特化版本成員函數(shù)" << 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()
}

輸出:

泛化版本成員函數(shù)
偏特化版本成員函數(shù)

2、從模板參數(shù)范圍:

template.h

#pragma once
#include <iostream>
#include <map>
 
template <typename T>
class TC3
{
public:
 void funtest()
 {
 std::cout << "泛化版本成員函數(shù)" << std::endl;
 }
};
 
template <typename T>
class TC3<const T>
{
public:
 void funtest()
 {
 std::cout << "const T偏特化版本成員函數(shù)" << std::endl;
 }
};
 
template <typename T>
class TC3<T&>
{
public:
 void funtest()
 {
 std::cout << "T&偏特化版本成員函數(shù)" << std::endl;
 }
};
 
template <typename T>
class TC3<T *>
{
public:
 void funtest()
 {
 std::cout << "T *偏特化版本成員函數(shù)" << 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();
}

輸出:

泛化版本成員函數(shù)
T&偏特化版本成員函數(shù)
T *偏特化版本成員函數(shù)
const T偏特化版本成員函數(shù)

 三、函數(shù)模板全特化(不能偏特化)

template.h

#pragma once
#include <iostream>
#include <map>
 
template <typename T, typename U>
void tfunc(T& a, U& b)
{
 std::cout << "tfunc 泛化版本函數(shù)" << std::endl;
}
 
template <>
void tfunc(int& a, int& b)
{
 std::cout << "tfunc 全特化版本函數(shù)" << 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 泛化版本函數(shù)
tfunc 全特化版本函數(shù)

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)頁標題:C++類模板、函數(shù)模板全特化、偏特化的使用
文章來源:http://muchs.cn/article32/jpecsc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、標簽優(yōu)化面包屑導航、企業(yè)建站、網(wǎng)站排名、網(wǎng)站維護

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司