c++11 函數模板的默認模板參數


c++11 函數模板的默認模板參數

 

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <vector>
#include <map>

// C++11之前,類模板是支持默認的模板參數,卻不支持函數模板的默認模板參數

//1、普通函數帶默認參數,c++98 編譯通過,c++11 編譯通過
void DefParm(int m = 3) {}

//2、類模板是支持默認的模板參數,c++98 編譯通過,c++11 編譯通過
template <typename T = int>
class DefClass {};

//3、函數模板的默認模板參數, c++98 - 編譯失敗,c++11 - 編譯通過
template <typename T = int>
void DefTempParm() {}

// 類模板的默認模板參數必須從右往左定義,函數模板的默認模板參數則沒這個限定
template<class T1, class T2 = int> class DefClass1;
template<class T1 = int, class T2> class DefClass2;   // 無法通過編譯

template<class T, int i = 0> class DefClass3;
template<int i = 0, class T> class DefClass4;         // 無法通過編譯

template<class T1 = int, class T2> void DefFunc1(T1 a, T2 b);
template<int i = 0, class T> void DefFunc2(T a);


void mytest()
{


    return;
}


int main()
{
    mytest();

    system("pause");
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM