C++ Template之非類型模板參數


非類型模板參數是通過基本變量類型引入,例如int,在使用時必須顯式自定值,不能通過推斷。

非類型模板參數的限制:不能是浮點數(在vc6.0上測試可以為浮點型),對象以及指向內部鏈接對象的指針。

#include <iostream>
#include <string>
#include <vector>
using namespace std;

enum COLOR{WHITE,BLACK};
template<COLOR name>//OK
int process (double v)
{
	return v*name;
}
template <const char* s>
class Myclass
{

};
const char s1[] = "hello";
extern const char s2[] = "hello";
int main()
{

	//Myclass<s1> m1;//ERROR 指向內部鏈接對象的指針
	//Myclass<"hello"> m2;
	Myclass<s2> m3;//OK指向外部鏈接對象的指針
	return 0;
}

 

 


免責聲明!

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



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