欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2020年02月07日 11:42:59   作者:jltxgcy  
這篇文章主要介紹了C++ 類模板、函數(shù)模板全特化、偏特化的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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

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

輸出:

泛化版本構(gòu)造函數(shù)
泛化版本成員函數(shù)
全特化版本構(gòu)造函數(shù)
全特化版本成員函數(shù)
泛化版本構(gòu)造函數(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語言學(xué)生成績管理系統(tǒng)課程設(shè)計word版

    C語言學(xué)生成績管理系統(tǒng)課程設(shè)計word版

    這篇文章主要為大家詳細(xì)介紹了C語言學(xué)生成績管理課程設(shè)計,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • 深入解析C++11?lambda表達(dá)式/包裝器/線程庫

    深入解析C++11?lambda表達(dá)式/包裝器/線程庫

    這篇文章主要介紹了C++11?lambda表達(dá)式/包裝器/線程庫的相關(guān)知識,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • C++小知識:不要節(jié)約代碼行數(shù)

    C++小知識:不要節(jié)約代碼行數(shù)

    今天小編就為大家分享一篇關(guān)于C++小知識:不要節(jié)約代碼行數(shù),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • C++中的復(fù)制構(gòu)造函數(shù)詳解

    C++中的復(fù)制構(gòu)造函數(shù)詳解

    今天小編就為大家分享一篇關(guān)于關(guān)于C++復(fù)制構(gòu)造函數(shù)的實現(xiàn)講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2021-09-09
  • Visual?C++?6.0新建一個C語言文件的圖文教程

    Visual?C++?6.0新建一個C語言文件的圖文教程

    本教程適用于C語言初學(xué)者,本文主要介紹了Visual?C++?6.0新建一個C語言文件的圖文教程,具有一定的參考價值,感興趣的可以了解一下
    2024-06-06
  • 簡單分析C語言中指針數(shù)組與數(shù)組指針的區(qū)別

    簡單分析C語言中指針數(shù)組與數(shù)組指針的區(qū)別

    這篇文章主要介紹了C語言中指針數(shù)組與數(shù)組指針的區(qū)別,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-11-11
  • C語言實現(xiàn)古代時辰計時與現(xiàn)代時間換算

    C語言實現(xiàn)古代時辰計時與現(xiàn)代時間換算

    這篇文章主要為大家詳細(xì)介紹了如何利用C語言實現(xiàn)古代時辰計時與現(xiàn)代時間換算,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-03-03
  • C語言結(jié)構(gòu)體的具體使用方法

    C語言結(jié)構(gòu)體的具體使用方法

    這篇文章主要介紹了C語言結(jié)構(gòu)體的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • C++11中的stoi & stod用法

    C++11中的stoi & stod用法

    這篇文章主要介紹了C++11中的stoi & stod用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 詳細(xì)聊一聊algorithm中的排序算法

    詳細(xì)聊一聊algorithm中的排序算法

    <algorithm>是C++標(biāo)準(zhǔn)程序庫中的一個頭文件,定義了C++?STL標(biāo)準(zhǔn)中的基礎(chǔ)性的算法(均為函數(shù)模板),下面這篇文章主要給大家介紹了關(guān)于algorithm中排序算法的相關(guān)資料,需要的朋友可以參考下
    2022-06-06

最新評論