在线视频网站 一级做爰片,南通网站建设找哪家,低成本网站制作,学做网站论目录C里面template怎么用inline函数模板类模板函数模板特化C里面template怎么用
template是什么? template其实是C的一种语法糖#xff0c;本意是去简化程序员的工作.
void swap(int *a,int *b){int temp *a;*a *b;*b temp;
}比如在写一个交换函数的的时候,参数为两个in…
目录C里面template怎么用inline函数模板类模板函数模板特化C里面template怎么用
template是什么? template其实是C的一种语法糖本意是去简化程序员的工作.
void swap(int *a,int *b){int temp *a;*a *b;*b temp;
}
比如在写一个交换函数的的时候,参数为两个int类型数据,那这个交换函数只能局限在int的数据类型吧float呢?double呢?这时候就需要写很多相同的代码,很无聊并且多余,这个时候我们就能使用我们今天的主角template来简化我们的写法了,改进之后 //声明时这样
templatetypename T void Swap(T *a, T *b){T temp *a;*a *b;*b temp;
}//调用时这样
int a10,b20;
swap(a,b);在这里的声明是隐式声明,后面会有讲到显式声明 inline函数模板
如果你想把你的函数声明为inline提高效率那么参考下面
inline关键字放在template之后函数类型之前
//正确写法
templatetypename Tinline T swap(const T*a,const T*b)//错误写法
inline templatetypename TT swap(const T*a,const T*b)类模板
//类模板声明
templateclass Typeclass Queue{
public:Queue();Type front();const Type front()const;void push(const Type );void pop();bool empty()const
private:...
};和咱的函数模板不同啊我们的类模板需要加个括号(显式)告诉编译器老子是啥类型。
//类模板定义
queueintqi;//什么是显式在定义时加个括号告诉他老子是int就是显式
Queuevectordoubleqc;
Queuestingqs;函数模板特化
有些时候我们编写的模板定义并不总是适用于所有类型
templatetypename T
int compare(const Tv1,const Tv2)
{if(v1v2)return -1;if(v1v2)return 1;return 0;
}当这个函数的参数类型是C风格字符串时这个函数不能正常工作(这个函数将比较两个指针在内存中的相对位置但没有比较指针指向对象的大小)这时候我们必须提供一个针对C风格字符串的特殊定义,这就是模板特化
函数模板的特化 特化的形式如下:
关键字tempalte后面接一对空的尖括号( )函数名后接模板名和一对尖括号尖括号中指定这个特化定义的模板形参函数形参表函数体
template
int compareconst char *(const char *const v1,const char *const v2)
{return strcmp(v1,v2);
}与任意函数一样函数模板特化可以声明而无须定义。
template
int compareconst char *(const char *const ,const char *const );一定要区分函数重载和模板特化,如果在特化中省略空的模板形参表template,那么结果是函数的重载而不是模板特化
int compare(const char *const,const char *const);
//声明了该函数的重载版本而不是模板版本在调用模板的这个特化版本之前特化的声明必须在作用域中
templateclass T
int compare(const Tt1,const T t2)
{...
}int main()
{int icompare(hello,world);...
}template
int compareconst char *(const char *const s1,const char *const s2)
{...
}
这个程序有错误这个函数将会调用未特化的原模板函数
特化出现在对该模板实例的调用之后是错误的
简单的总结:函数模板就是一个通用的函数入口,处理大部分类型的数据,模板特化就是为了处理少数的函数模板里的规则不能处理的数据类型.