C++實現(xiàn)模板中的非類型參數(shù)的方法
非類型模板參看,顧名思義,模板參數(shù)不限定于類型,普通值也可作為模板參數(shù)。在基于類型的模板中,模板實例化時所依賴的是某一類型的模板參數(shù),你定義了一些模板參數(shù)(template<typename T>)未加確定的代碼,直到模板被實例化這些參數(shù)細(xì)節(jié)才真正被確定。而非類型模板參數(shù),面對的未加確定的參數(shù)細(xì)節(jié)是指(value),而非類型。當(dāng)要使用基于值的模板時,你必須顯式地指定這些值,模板方可被實例化。
在函數(shù)模板中使用非類型參數(shù)
#include<iostream>
using namespace std;
//在函數(shù)模板中使用非類型參數(shù)
template<class T>void Swap(T &a, T &b);
template<typename T, unsigned N>void Swap(T (&a)[N],T (&b)[N]);
template<typename T, unsigned N>void printArray(T (&arr)[N]);
int main(){
int m = 10, n = 90;
Swap(m,n);
cout << "m = " << m << ", n = " << n << endl;
int a[5] = { 1, 2, 3, 4, 5 };
int b[5] = { 10, 20, 30, 40, 50 };
Swap(a, b);
printArray(a);
printArray(b);
return 0;
}
template<class T> void Swap(T &a,T &b){
T temp = a;
a = b;
b = temp;
}
template<class T, unsigned N> void Swap(T (&a)[N],T (&b)[N]){
T temp;
for (int i = 0; i < N;i++){
temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}
template<typename T, unsigned N>void printArray(T (&arr)[N]){
for (int i = 0; i < N;i++){
if (i == N-1){
cout << arr[i] << endl;
}
else{
cout << arr[i] << ", ";
}
}
}
在類模板中使用非類型參數(shù)
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
//動態(tài)數(shù)組實現(xiàn),在類模板中使用非類型參數(shù)
template<typename T,int N>
class Array{
public:
Array();
~Array();
public:
T & operator[](int i);//重載下標(biāo)運算符
int length() const{ return m_length; }//獲取數(shù)組長度
bool capacity(int n);//是否可改變數(shù)組容量
private:
int m_length;//數(shù)組當(dāng)前長度
int m_capacity;//當(dāng)前內(nèi)存容量
T *m_p;//指向數(shù)組內(nèi)存的指針
};
template<typename T,int N>
Array<T, N>::Array(){
m_p = new T[N];
m_capacity = m_length = N;
}
template<typename T,int N>
Array<T, N>::~Array(){
delete[] m_p;
}
template<typename T,int N>
T & Array<T, N>::operator[](int i){
if (i<0||i>=m_length){
cout << "Exception:Array index out of bounds!" << endl;
}
return m_p[i];
}
template<typename T,int N>
bool Array<T, N>:: capacity(int n){
if (n>0){
int len = m_length + n;
if (len<=m_capacity){
m_length = len;
return true;
}
else{
T *pTemp = new T[m_length + 2 * n*sizeof(T)];
if (NULL==pTemp){
cout << "Exception: Failed to allocate memory!";
return false;
}
else{
memcpy(pTemp,m_p,m_length*sizeof(T));
delete[] m_p;
m_p = pTemp;
m_capacity = m_length = len;
}
}
}
else{
int len = m_length - abs(n);
if (len<0){
cout << "Exception:Array length is too small!" << endl;
return false;
}
else{
m_length = len;
return true;
}
}
}
int main(){
Array<int, 5> arr;
for (int i = 0, len = arr.length(); i < len;i++){
arr[i] = 2 * i;
}
cout << "first print:" << endl;
for (int i = 0, len = arr.length(); i < len;i++){
cout << arr[i] << " ";
}
cout << endl;
//擴大容量為增加的元素賦值
arr.capacity(8);
for (int i = 5, len = arr.length(); i < len;i++){
arr[i] = 2 * i;
}
cout << endl;
cout << "second print:" << endl;
for (int i = 0, len = arr.length(); i < len;i++){
cout << arr[i] << " ";
}
cout << endl;
arr.capacity(-4);
cout << "third print: " << endl;
for (int i = 0, len = arr.length(); i < len; i++){
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
非類型模板參數(shù)的限制
非類型模板參數(shù)是有類型限制的。一般而言,它可以是常整數(shù)(包括enum枚舉類型)或者指向外部鏈接對象的指針。
浮點數(shù)和類對象(class-type)不允許作為非類型模板參數(shù):
template<double VAL> // ERROR: 浮點數(shù)不可作為非類型模板參數(shù)
double process(double v)
{
return v * VAL;
}
template<std::string name> // ERROR:類對象不能作為非類型模板參數(shù)
class MyClass
{}
稍作變通,我們即可使編譯通過:
template<double* PVAL>
double process(const double& x)
{
return x * (*PVAL);
}
template<const char* name>
class MyClass
{
...
}
這樣可順利通過編譯,但如果想在當(dāng)前文件中使用這兩個模板,還需要動一些手腳:
double val = 10; double res = process<&val>(20); // ERROR: 表達式必須含有常量值 MyClass<"hello"> x; // ERROR: 模板參數(shù)不能引用非外部實體 const char* s = "hello"; MyClass<s> x; // ERROR: 表達式必須含有常量值
這里就點出另外一點注意事項,也就是非類型模板參數(shù)的限制,非類型模板參數(shù)可以是指針,但該指針必須指向外部鏈接對象,還記得在A.cpp中如何引用B.cpp中的全局變量嗎,在A.hpp中使用extern關(guān)鍵字對外部變量加以引用。
// B.cpp double val = 3.14159265; char str[] = "hello";
// A.hpp extern double val; extern char str[];
// A.cpp #include "A.hpp" double res = process<&val>(10); MyClass<str> x;
到此這篇關(guān)于C++實現(xiàn)模板中的非類型參數(shù)的方法的文章就介紹到這了,更多相關(guān)C++ 模板非類型參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++?Boost?ProgramOptions超詳細(xì)講解
Boost是為C++語言標(biāo)準(zhǔn)庫提供擴展的一些C++程序庫的總稱。Boost庫是一個可移植、提供源代碼的C++庫,作為標(biāo)準(zhǔn)庫的后備,是C++標(biāo)準(zhǔn)化進程的開發(fā)引擎之一,是為C++語言標(biāo)準(zhǔn)庫提供擴展的一些C++程序庫的總稱2022-11-11
C語言植物大戰(zhàn)數(shù)據(jù)結(jié)構(gòu)快速排序圖文示例
這篇文章主要為大家介紹了C語言植物大戰(zhàn)數(shù)據(jù)結(jié)構(gòu)快速排序圖文示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05

