C++实现模板中的非类型参数的方法

非类型模板参看,顾名思义,模板参数不限定于类型,普通值也可作为模板参数。在基于类型的模板中,模板实例化时所依赖的是某一类型的模板参数,你定义了一些模板参数(template<typename T>)未加确定的代码,直到模板被实例化这些参数细节才真正被确定。而非类型模板参数,面对的未加确定的参数细节是指(value),而非类型。当要使用基于值的模板时,你必须显式地指定这些值,模板方可被实例化。

在函数模板中使用非类型参数

#include<iostream>using namespace std;//在函数模板中使用非类型参数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] << ", "; } }}

在类模板中使用非类型参数

#include<iostream>#include<cstring>#include<cstdlib>using namespace std;//动态数组实现,在类模板中使用非类型参数template<typename T,int N>class Array{public: Array(); ~Array();public: T & operator[](int i);//重载下标运算符 int length() const{ return m_length; }//获取数组长度 bool capacity(int n);//是否可改变数组容量private: int m_length;//数组当前长度 int m_capacity;//当前内存容量 T *m_p;//指向数组内存的指针};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;}

非类型模板参数的限制

非类型模板参数是有类型限制的。一般而言,它可以是常整数(包括enum枚举类型)或者指向外部链接对象的指针。

浮点数和类对象(class-type)不允许作为非类型模板参数:

template<double VAL>      // ERROR: 浮点数不可作为非类型模板参数double process(double v){  return v * VAL;}template<std::string name>   // ERROR:类对象不能作为非类型模板参数class MyClass{}稍作变通,我们即可使编译通过:template<double* PVAL>double process(const double& x){  return x * (*PVAL);}template<const char* name>class MyClass{  ...}

这样可顺利通过编译,但如果想在当前文件中使用这两个模板,还需要动一些手脚:

double val = 10;double res = process<&val>(20);   // ERROR: 表达式必须含有常量值MyClass<"hello"> x;         // ERROR: 模板参数不能引用非外部实体const char* s = "hello";MyClass<s> x;            // ERROR: 表达式必须含有常量值

这里就点出另外一点注意事项,也就是非类型模板参数的限制,非类型模板参数可以是指针,但该指针必须指向外部链接对象,还记得在A.cpp中如何引用B.cpp中的全局变量吗,在A.hpp中使用extern关键字对外部变量加以引用。

// B.cppdouble val = 3.14159265;char str[] = "hello";
// A.hppextern double val;extern char str[];
// A.cpp#include "A.hpp"double res = process<&val>(10);MyClass<str> x;

到此这篇关于C++实现模板中的非类型参数的方法的文章就介绍到这了,更多相关C++ 模板非类型参数内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

生活会变成什么样子?正因为时光流逝一去不复返,

C++实现模板中的非类型参数的方法

相关文章:

你感兴趣的文章:

标签云: