2023-2-19-What is ‘ template<typename E, E V> ‘?

简介: 2023-2-19-What is ‘ template<typename E, E V> ‘?

C++里面template怎么用

template是什么?

template其实是C++的一种语法糖,本意是去简化程序员的工作.

void swap(int *a,int *b){
    int temp = *a;
    *a = *b;
    *b = temp;
}

比如在写一个交换函数的的时候,参数为两个int类型数据,那这个交换函数只能局限在int的数据类型吧,float呢?double呢?这时候就需要写很多相同的代码,很无聊并且多余,这个时候我们就能使用我们今天的主角template来简化我们的写法了,改进之后

//声明时这样
template<typename T> void Swap(T *a, T *b){
    T temp = *a;
    *a = *b;
    *b = temp;
}
//调用时这样
int a=10,b=20;
swap(&a,&b);

在这里的声明是隐式声明,后面会有讲到显式声明


inline函数模板

如果你想把你的函数声明为inline提高效率,那么参考下面

inline关键字放在template之后,函数类型之前

//正确写法
template<typename T>inline T swap(const T*a,const T*b)
//错误写法
inline template<typename T>T swap(const T*a,const T*b)

类模板

//类模板声明
template<class Type>class Queue{
public:
    Queue();
    Type &front();
    const Type &front()const;
    void push(const Type &);
    void pop();
    bool empty()const
private:
    ...
};

和咱的函数模板不同啊,我们的类模板需要加个括号(显式)告诉编译器老子是啥类型。

//类模板定义
queue<int>qi;//什么是显式,在定义时加个括号告诉他老子是int就是显式
Queue<vector<double>>qc;
Queue<sting>qs;

函数模板特化

有些时候,我们编写的模板定义并不总是适用于所有类型

template<typename T>
int compare(const T&v1,const T&v2)
{
    if(v1<v2)return -1;
    if(v1>v2)return 1;
    return 0;
}

当这个函数的参数类型是C风格字符串时,这个函数不能正常工作(这个函数将比较两个指针在内存中的相对位置,但没有比较指针指向对象的大小),这时候我们必须提供一个针对C风格字符串的特殊定义,这就是模板特化

函数模板的特化

特化的形式如下:

  • 关键字tempalte后面接一对空的尖括号(< >)
  • 函数名后接模板名和一对尖括号,尖括号中指定这个特化定义的模板形参
  • 函数形参表
  • 函数体
template<>
int compare<const char *>(const char *const &v1,const char *const &v2)
{
    return strcmp(v1,v2);
}

与任意函数一样,函数模板特化可以声明而无须定义。

template<>
int compare<const char *>(const char *const &,const char *const &);

一定要区分函数重载和模板特化,如果在特化中省略空的模板形参表template<>,那么结果是函数的重载而不是模板特化

int compare(const char *const&,const char *const&);
//声明了该函数的重载版本而不是模板版本

在调用模板的这个特化版本之前,特化的声明必须在作用域中

template<class T>
int compare(const T&t1,const T& t2)
{
    ...
}
int main()
{
    int i=compare("hello","world");
    ...
}
template<>
int compare<const char *>(const char *const &s1,const char *const &s2)
{
    ...
}

这个程序有错误,这个函数将会调用未特化的原模板函数

特化出现在对该模板实例的调用之后是错误的

简单的总结:函数模板就是一个通用的函数入口,处理大部分类型的数据,模板特化就是为了处理少数的函数模板里的规则不能处理的数据类型.


目录
相关文章
|
26天前
|
算法 C++ 容器
C++中模板函数以及类模板的示例(template)
C++中模板函数以及类模板的示例(template)
|
1月前
|
编译器 测试技术 调度
C++ 中 template<class T>和template<typename T>的区别
C++ 中 template<class T>和template<typename T>的区别
11 0
|
1月前
|
编译器 C++
c++中typename和class的区别
c++中typename和class的区别
27 0
|
7月前
|
编译器 程序员 C++
[Eigen中文文档] C++中的template和typename关键字
在C++中,template和typename关键字有两种用途。其中一个在程序员中相当有知名度:用于定义模板。另一个用法则更为隐晦:用于指定一个表达式是引用模板函数还是类型。这经常困扰使用Eigen库的程序员,通常会导致编译器难以理解的错误信息,比如 expected expression 或 no match for operator<>。
89 0
|
10月前
|
存储 编译器 C++
【C++模板】——template
【C++模板】——template
|
程序员 编译器 C++
C++模板(关键字template,typename)介绍
C++模板(关键字template,typename)介绍
151 0
ɵɵtemplate和ng-template的对应关系
ɵɵtemplate和ng-template的对应关系
ɵɵtemplate和ng-template的对应关系
|
C++ 程序员 编译器
理解模板类型推断(template type deduction)
理解模板类型推断(template type deduction) 我们往往不能理解一个复杂的系统是如何运作的,但是却知道这个系统能够做什么。C++的模板类型推断便是如此,把参数传递到模板函数往往能让程序员得到满意的结果,但是却不能够比较清晰的描述其中的推断过程。
1364 0
|
C++ 程序员 编译器
现代C++之理解模板类型推断(template type deduction)
现代C++之理解模板类型推断(template type deduction)目录 ParamType是指针或者引用类型ParamType是一个Universal ReferenceParamType既不是指针也不是引用数组参数函数参数要点总结 正文 理解模板类型推断(template type deduction)我们往往不能理解一个复杂的系统是如何运作的,但是却知道这个系统能够做什么。
1558 0