经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
c++ 模板详解
来源:cnblogs  作者:白菜茄子  时间:2023/6/9 11:46:09  对本文有异议
  • 模板就是将类型进行参数化

函数模板

  1. //函数模板的定义格式
  2. template<class 形参名,class 形参名...>
  3. 返回值类型 函数名(参数列表){
  4. 函数体;
  5. }
  • 模板形参不能为空,并且函数模板中每一个类型参数在函数参数表中至少使用一次,只有这样才能推断出具体的类型
  1. template <class T>
  2. T Add(T x1, T x2){
  3. return x1+x2;
  4. }
  5. int main(){
  6. cout<<Add(2,3)<<endl;//5
  7. cout<<Add(3.2,1.4)<<endl;//4.6
  8. return 0;
  9. }

函数模板实例化

  • 不能直接使用函数模板实现具体操作,必须对模板进行实例化,即将模板参数实例化,就是用具体的类型参数去替换函数模板中的模板参数,生成一个确定的具体类型的真正函数,才能实现运算操作

  • 实例化方式

    • 隐式实例化:根据具体的函数调用形式,推演出模板参数类型
    • 显式实例化:通过显式声明形式指定模板参数类型
  • 隐式实例化

    • 上述代码中调用Add(2,3)的过程

    • 编译器根据传入的实参2和3推断出模型形参类型是int

    • 会将函数模板实例化出一个int类型的函数

      1. int Add(int x1,int x2){
      2. return x1+x2;
      3. }
    • 编译器生成具体类型函数的过程称为实例化,生成的函数称为模板函数

    • 生成int类型的函数后,再将2和3传入进行计算

    • 当调用Add(3.2,1.4)又会生成一个float的函数

    • 每次调用都会根据不同的类型实例化出不同类型的函数,所以最终可执行程序的大小和重载方式相比并不会减少,只是提高了程序员对代码的复用

    • 问题:隐式实例化不能为同一个模板形参指定两种不同的类型,例如Add(2,3.2),此时编译器会报错,因为编译器不能推断出T的类型

  • 显式实例化

      1. cout<<Add<int>(2,3.2)<<endl;//5 cout << Add<int>(2, static_cast<int>(3.2)) << endl;最好的方式是进行一个显式转换
      2. cout<<Add<float>(3,2)<<endl;//5
  • 函数模板也可以进行重载

  1. template <class T>
  2. T Add(T x1, T x2){
  3. return x1+x2;
  4. }
  5. template <class T>
  6. T Add(T x1, T x2, T x3){
  7. return x1 + x2 + x3;
  8. }
  9. int main(){
  10. cout<<Add(2,3)<<endl;//5
  11. cout<<Add(3.2,1.4)<<endl;//4.6
  12. cout<<Add<int>(2,3.2)<<endl;//5 cout << Add<int>(2, static_cast<int>(3.2)) << endl;最好的方式是进行一个显式转换
  13. cout<<Add<float>(3,2)<<endl;//5
  14. cout<<Add(1,2,3)<<endl;//6
  15. return 0;
  16. }
  • 当模板函数和自己写的Add具有具体的int类型的函数而言,此时如果传入的参数是int则会调用具体化的int函数,即C++编译器优先考虑普通函数

类模板

  1. //类模板定义格式
  2. template <class 形参名, class 形参名>
  3. class 类名{
  4. }
  • 由于类模板包含类型参数,因此也称为参数化类,如果说类是对象的抽象,对象是类的实例,则类模板是类的抽象,类是类模板的实例
  1. template <class T1, class T2>
  2. class Add{
  3. private:
  4. T1 x1;
  5. T2 x2;
  6. public:
  7. Add(T1 x1, T2 x2):x1(x1),x2(x2){}
  8. T1 get(){
  9. return x1 + x2;
  10. }
  11. };
  12. int main(){
  13. Add<double,int> a(1.2,2);
  14. cout<<a.get()<<endl;//3.2
  15. return 0;
  16. }

类模板实例化

  • 使用类模板创建对象时,必须指明具体的数据类型,这和函数模板不一样,这是因为函数模板中一定会传递实参,可以根据实参推断出所有的数据类型,但是类模板中,构造函数时不一定会同时传递T1 T2,所以不一定会百分百推断出数据类型,因此需要显式传递
  • 可以使用对象指针进行实例化Add<double,int> *p1 = new Add<double,int>(1.2,2);但是需要注意两边的模板参数需要相同
  1. template <class T>
  2. class A{
  3. public:
  4. T Add(T t1, T t2){
  5. return t1 + t2;
  6. }
  7. };
  8. int main(){
  9. A<int> a;
  10. cout<<a.Add(1.2,2)<<endl;
  11. }
  • 在函数模板中,不能进行自动转换,这是因为函数模板中需要根据实参来推断数据类型,而在类模板中,因为已经显式指定了int,所以会创建一个int的类,然后可以自动转换

  • 在类模板外定义成员函数,格式

  1. template <模板形参表>
  2. 函数返回类型 类名<模板形参名>::函数名(参数列表){}
  1. template <class T>
  2. class A{
  3. public:
  4. T Add(T t1, T t2){
  5. return t1 + t2;
  6. }
  7. T sub(T t1, T t2);
  8. };
  9. template<class T>
  10. T A<T>::sub(T t1, T t2) {
  11. }
  • 类模板在实例化时,带有模板形参的成员函数并不对着自动被实例化,只有当它被调用或取地址时才会被实例化。因为不被调用的时候,你根本不知道模板形参表中的数据类型是什么,想实例化就需要把所有的数据类型组合都实例化一遍,这显然是不可接受的

类模板与友元函数

非模板友元函数

  • 在类模板中声明一个普通的友元函数,该函数时类模板所有实例的友元函数,可以访问全局对象,也可以使用全局指针访问非全局对象,可以创建自己的对象,也可以访问独立于对象模板的静态数据成员
  1. #include <iostream>
  2. using namespace std;
  3. template <class T>
  4. class A{
  5. private:
  6. T x1,x2;
  7. static T x3;
  8. public:
  9. A(T x1,T x2):x1(x1),x2(x2){}
  10. friend void func();
  11. };
  12. //这里进行特化赋值
  13. template<>
  14. int A<int>::x3 = 10;
  15. template<>
  16. double A<double>::x3 = 100;
  17. void func(){
  18. cout<<A<int>::x3<<endl;//10
  19. cout<<A<double>::x3<<endl;//100
  20. A<char> a('A','a');
  21. cout<<a.x1<<" "<<a.x2<<endl;//A a
  22. }
  23. int main(){
  24. func();
  25. return 0;
  26. }
  • 如果func()中有一个特定类型的参数,则只能访问特定类型的数据
  1. #include <iostream>
  2. using namespace std;
  3. template <class T>
  4. class A{
  5. private:
  6. T x1,x2;
  7. static T x3;
  8. public:
  9. A(T x1,T x2):x1(x1),x2(x2){}
  10. friend void func(A<T> a1);
  11. };
  12. //这里进行特化赋值
  13. template<>
  14. int A<int>::x3 = 10;
  15. template<>
  16. double A<double>::x3 = 100;
  17. void func(A<int> b){
  18. cout<<A<int>::x3<<endl;//10
  19. // cout<<A<double>::x3<<endl;//100此时就不可以访问double类型了
  20. A<char> a('A','a');//可以正常创建别的类型的对象,因为这个创建是在任何地方都可以
  21. // cout<<a.x1<<" "<<a.x2<<endl;//A a 这里也不可以访问了,因为不是char类型的友元函数
  22. }
  23. int main(){
  24. func(A<int>(1,2));
  25. return 0;
  26. }

约束模板友元函数

  • 这样的友元函数本身就是一个函数模板,但其实例化类型取决于类被实例化时的类型(被约束),每个类的实例化都会产生一个与之匹配的具体化的友元函数
  1. #include <iostream>
  2. using namespace std;
  3. template <class T>
  4. void func(T x1, T x2);
  5. template <class T>
  6. class A{
  7. private:
  8. T x1,x2;
  9. public:
  10. A(T x1, T x2):x1(x1),x2(x2){}
  11. friend void func<T>(T x1,T x2);//这里也可以写成friend void func<>(T x1, T x2);因为可以推断出T的类型来
  12. };
  13. template <class T>
  14. void func(T x1,T x2){
  15. A<T> a1(x1,x2);
  16. cout<<a1.x1<<" "<<a1.x2<<endl;
  17. cout<<sizeof(A<T>)<<endl;
  18. }
  19. int main(){
  20. func(1,2);//1 2 8
  21. func<double>(1,2);// 1 2 16
  22. }

非约束模板友元函数

  • 在类内部声明友元函数模板,友元函数的模板形参与类模板的形参没有关系,此时友元函数为类模板的非约束模板友元函数
  1. #include <iostream>
  2. using namespace std;
  3. template <class T>
  4. void func(T x1, T x2);
  5. template <class T>
  6. class A{
  7. private:
  8. T x1,x2;
  9. public:
  10. A(T x1, T x2):x1(x1),x2(x2){}
  11. template<class U,class V>
  12. friend void func(U u, V v);
  13. };
  14. template <class U, class V>
  15. void func(U u, V v){
  16. cout<<u.x1<<endl;
  17. cout<<v.x1<<endl;
  18. }
  19. int main(){
  20. A<int> a1(1,2);
  21. A<double> a2(2.3,4.5);
  22. func(a1,a2);//1 2.3
  23. }

模板的特化

  • 模板特化:在原模板类的基础上,针对特殊类型所进行的特殊化的实现,分为函数模板特化和类模板特化

类模板特化

  • 类模板的特化分为全特化和偏特化
    • 全特化:对类模板参数列表的类型全部都确定
    • 偏特化:对类模板的参数列表中的部分参数进行确定化。分为部分特化和参数进一步限制
  1. #include <iostream>
  2. using namespace std;
  3. template <class A, class B>
  4. class C{
  5. public:
  6. C(){
  7. cout<<"template<class A, class B> class C"<<endl;
  8. }
  9. };
  10. //全特化
  11. template <>
  12. class C<int,double>{
  13. public:
  14. C(){
  15. cout<<"template<> class C<int,double>"<<endl;
  16. }
  17. };
  18. //偏特化
  19. template <class A>
  20. class C<A,int>{
  21. public:
  22. C(){
  23. cout<<"template<class A> class C<A,int>"<<endl;
  24. }
  25. };
  26. //偏特化不一定指的是特化部分参数,而是对模板类型的进一步限制
  27. template <class A, class B>
  28. class C<A*,B*>{
  29. public:
  30. C(){
  31. cout<<"template<class A,class B> class C<A*,B*>"<<endl;
  32. }
  33. };
  34. int main(){
  35. C<int,int> c1;//template<class A> class C<A,int>
  36. C<double,double> c2;//template<class A, class B> class C
  37. C<int,double> c3;//template<> class C<int,double>
  38. C<int*,double> c4;//template<class A, class B> class C
  39. C<int*,double*> c5;//template<class A,class B> class C<A*,B*>
  40. }

函数模板特化

  • 函数模板只支持全特化,不支持偏特化
  1. #include <iostream>
  2. using namespace std;
  3. template <class T1,class T2>
  4. int compare(const T1& v1, const T2& v2){
  5. cout<<"template <class T>"<<endl;
  6. return 0;
  7. }
  8. template <>
  9. int compare<int,int>(const int& v1, const int& v2){
  10. cout<<"template<>"<<endl;
  11. return 0;
  12. }
  13. //不支持偏特化
  14. //template <class T1>
  15. //int compare<T1,int>(const T1& v1, const int& v2){
  16. // cout<<"template<class T1>"<<endl;
  17. // return 0;
  18. //}
  19. int main(){
  20. compare(1,1);//template<>
  21. compare(1.2,2.3);//template <class T>
  22. }
  • 但是函数偏特化的功能也不是不能实现,并且有两种方式进行实现
  1. //方式1
  2. //将第二个参数为int的情况排除掉
  3. //然后再写一个专门的一个参数的模板,这样可以实现函数模板的功能
  4. #include <iostream>
  5. #include <type_traits>
  6. template <typename A, typename B>
  7. typename std::enable_if<!std::is_same<B, int>::value>::type f(A a, B b) {
  8. std::cout << "template <typename A, typename B>" << std::endl;
  9. }
  10. template <typename A>
  11. void f(A a, int b) {
  12. std::cout << "template <typename A>" << std::endl;
  13. }
  14. int main() {
  15. f(10, 5); // template <typename A>
  16. f(10, 5.5); // template <typename A, typename B>
  17. return 0;
  18. }
  1. //方式2
  2. //使用结构体进行封装
  3. #include <iostream>
  4. using namespace std;
  5. template<class A,class B>
  6. struct C{
  7. C(A a,B b){}
  8. void operator()() {
  9. std::cout << "template<class A,class B>" << std::endl;
  10. }
  11. };
  12. template <typename A>
  13. struct C<A,int>{
  14. C(A a,int b){}
  15. void operator()() {
  16. std::cout << "template <typename A>" << std::endl;
  17. }
  18. };
  19. int main(){
  20. C<int,int>(10,5)();//template <typename A>
  21. C<int,double>(10,5.5)();//template<class A,class B>
  22. return 0;
  23. }

原文链接:https://www.cnblogs.com/sunjianzhao/p/17468814.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号