C++ template

C++ template
函数模板:
1,模板函数为不同的模板实参定义了一个函数家族
2,当传递模板实参的时候,可以根据实参的类型对函数模板实例化
3,可以显示指定模板参数。
4,可以重载函数模板
5,当重载函数模板时,可以显示的指定模板参数
6,函数模板的所有重载版本的声明一定位于调用之前
类模板:
1,类模板成员函数只有在使用的时候才会被实例化。为了使用类模板对象,必须显示的指定模板实参。
    <<> >:后面加空格的原因是编译器会认为是使用operator>> 产生编译错误
2,类模板的特化跟函数重载类似,template<>,class stack<std::string>{};成员函数要写为普通函数
3,模板类的缺省参数 template<<typename T1,typename T2=std::vector<T> >,传入单个参数则默认vector,可指定另一个参数
非类型模板参数:
1,模板可以具有值模板参数,而不仅是类型模板参数
2,对于非类型模板参数,不能用浮点型,class类型的对象和内部链接对象
技巧性基础知识
1,当某个依赖于模板参数的名称是一个类型时,应该使用typname;
2,函数模板并不支持模板的模板参数
3,显示的调用内建类型的缺省构造函数
4,if要访问依赖于模板参数的类型名称,应在类型名称前添加typename
5,嵌套类和成员函数可以是模板
6,类模板也可以作为模板参数称为模板的模板参数;模板的模板实参必须精确的匹配
7,通过显示调用缺省的构造函数,可以确保模板的变量和成员都已用一个缺省值完成初始化
模板实战
1,模板是位于宏和普通声明之间的一种构造
2,对于模板采用对待宏或内联函数的解决办法,把声明和定义位于同一个文件中(或定义时显示实例化);或在头文件末尾添加.cpp;非内联函数在调用的位置不会扩展,而是基于实例化后的函数拷贝
3,export(导出)可以用于函数模板,类模板的成员函数,成员函数模板,和类模板的静态数据成员的声明,类模板的声明(意味着类成员都可作为导出实体)从而实现,声明与定义的分离,称为分离模板
4,export和inline不可同时使用
5,组织模板代码的方法:包含模型,显示实例化和分离模型;
6,在模板声明代码和模板定义代码放在不同的头文件中可以在包含和显示实例化之间做出选择
模板术语
1,模板的特化:实例化是其一种方式;对模板参数进行特定的替换;局部特化
2,声明时将一个名称引入某个作用域;定义则分配了内存空间
3,对于变量而言,进行初始化和不具有extern关键字的声明都是定义
4,模板参数是位于模板声明或定义内部,关键字template后面
深入模板基础
1,成员函数模板不能被声明为虚函数
模板中的名称
1,x< ::C>e;<:是另一种格式,所以空格是必须的
2,c++编译器会使用一张符号表把扫描器和解析器结合起来,当解析某个声明时,该声明会添加到表中。当扫描器找到一个标识符时,它会在符号表中进行查找,如果发现该标识符是一个类型,就会注释
这个所获的的标记。
模板多态的威力
c++和C中,函数调用实参在缺省情况下都是以“传值”的方式进行传递的

C++ templates are a powerful feature of the C++ programming language that allow generic programming. Templates enable the creation of functions and classes that can work with different data types without the need for separate implementations for each data type. Templates are defined using the keyword "template" followed by a list of template parameters enclosed in angle brackets "< >". The template parameters can be either type parameters or non-type parameters, depending on whether they represent a data type or a value. For example, a type parameter might be used to specify the data type of a container class, while a non-type parameter might be used to specify the size of an array. Here is an example of a simple function template that returns the maximum of two values: ```c++ template<typename T> T max(T a, T b) { return a > b ? a : b; } ``` In this example, the "typename" keyword is used to indicate that T is a type parameter. The function can be used with any data type for which the ">" operator is defined. Templates can also be used to define class templates, which are similar to regular classes but can work with different data types. Here is an example of a simple class template for a stack: ```c++ template<typename T> class Stack { public: void push(T value); T pop(); private: std::vector<T> data_; }; template<typename T> void Stack<T>::push(T value) { data_.push_back(value); } template<typename T> T Stack<T>::pop() { T value = data_.back(); data_.pop_back(); return value; } ``` In this example, the class template is defined with a single type parameter T. The member functions push and pop are defined outside the class definition using the scope resolution operator "::". Templates are a powerful tool that can greatly simplify code and make it more reusable. However, they can also be complex and difficult to debug. It is important to use templates judiciously and to thoroughly test them with a variety of data types.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值