• 浅谈C++中的auto和decltype


    其实auto和decltype都是用来自动识别类型的,那么我们就会有疑问:我们在定义变量的时候不是直接给出变量类型吗? 那么还为什么需要类型推导呢?
    这是因为在定义变量时,必须先给出变量的实际类型,编译器才允许定义,但有些情况下可能不知道需要实际类型怎么给,或者类型写起来特别复杂,比如:
    1. #include<iostream>
    2. #include <map>
    3. #include <string>
    4. using namespace std;
    5. int main()
    6. {
    7. short a = 32670;
    8. short b = 32670;
    9. // c如果给成short,会造成数据丢失,如果能够让编译器根据a+b的结果推导c的实际类型,就不会存在问题
    10. short c = a + b;
    11. std::map<std::string, std::string> m{ { "apple", "苹果" }, { "banana", "香蕉" } };
    12. // 使用迭代器遍历容器, 迭代器类型太繁琐
    13. std::map<std::string, std::string>::iterator it = m.begin();
    14. while (it != m.end())
    15. {
    16. cout << it->first << " " << it->second << endl;
    17. ++it;
    18. }
    19. return 0;
    20. }

    对于上面的这两种情况我们可以用auto和decltype来解决:

    1. #include<iostream>
    2. #include<string>
    3. #include<map>
    4. using namespace std;
    5. int main()
    6. {
    7. short a = 32670;
    8. short b = 32670;
    9. auto c = a + b;//这里直接用auto来识别c的类型
    10. decltype(a + b) d = a + b;//或者用decltype来识别a+b的得到的结果类型,然后来定义c的类型
    11. cout << typeid(decltype(a + b)).name ()<< endl;
    12. cout << typeid(c).name() << endl;
    13. cout << endl;
    14. std::map<std::string, std::string> m{ { "apple", "苹果" }, { "banana", "香蕉" } };
    15. auto it1 = m.begin();//可以直接用auto来识别迭代器的类型
    16. while (it1 != m.end())
    17. {
    18. cout << it1->first << " " << it1->second << endl;
    19. ++it1;
    20. }
    21. cout << endl;
    22. decltype(m.begin()) it2=m.begin();
    23. while (it2 != m.end())
    24. {
    25. cout << it2->first << " " << it2->second << endl;
    26. ++it2;
    27. }
    28. return 0;
    29. }

    运行结果:

    下面我们来具体介绍auto和decltype 

    1. auto

    1.1 对于auto的一些使用规则

    1.对于用auto修饰的变量一定要初始化

    1. int TestAuto()
    2. {
    3. return 10;
    4. }
    5. int main()
    6. {
    7. int a = 10;
    8. auto b = a;
    9. auto c = 'a';
    10. auto d = TestAuto();
    11. cout << typeid(b).name() << endl;
    12. cout << typeid(c).name() << endl;
    13. cout << typeid(d).name() << endl;
    14. //auto e; 无法通过编译,使用auto定义变量时必须对其进行初始化
    15. return 0;
    16. }

    运行结果:可以看到auto可以自动识别变量的类型

    2.  注意auto是不可以作为函数的参数和返回值的

    这是因为用auto修饰的变量必须要初始化,这样才能在编译阶段推断出变量的类型,而函数的参数和返回值在程序运行阶段才能确定,所以auto是不可以作为函数的参数和返回值的。 

     3. auto与指针和引用结合起来使用

    auto声明指针类型时,用autoauto*没有任何区别,但用auto声明引用类型时则必须加&

     4. 在同一行定义多个变量

    当在同一行声明多个变量时,这些变量必须是相同的类型,否则编译器将会报错,因为编译器实际只对 第一个类型进行推导,然后用推导出来的类型定义其他变量

    1. void TestAuto()
    2. {
    3. auto a = 1, b = 2;
    4. auto c = 3, d = 4.0; // 该行代码会编译失败,因为c和d的初始化表达式类型不同
    5. }

     5.auto不能用来声明数组

     我们在了解了auto的一些基本用法那么我们来解决一下上面我们最一开始提出来的问题:

    2. decltype

    那么有了auto为什么还要有decltype呢?

    这是因为:auto使用的前提是:必须要对auto声明的类型进行初始化,否则编译器无法推导出auto的实际类型。但有时候可能需要根据表达式运行完成之后结果的类型进行推导,因为编译期间,代码不会运行,此时auto也就无能为力。

    我们来看一下decltype的常见用法

    1. #include<iostream>
    2. using namespace std;
    3. void* test(int a)
    4. {
    5. return new int(a);
    6. }
    7. typedef decltype(test)* func;//可以通过推导函数类型来定义函数指针
    8. int main()
    9. {
    10. short a = 32670;
    11. short b = 32670;
    12. //可以直接通过a+b的结果来推断出d的类型,同时我们发现它可以给用a+b的类型来给多个变量赋值
    13. //而auto仅仅只能通过变量的初始值给一个变量来确定类型
    14. decltype(a + b) d = a + b;
    15. cout << typeid(decltype(test)).name() << endl;//推断test函数的类型
    16. cout << typeid(func).name() << endl;//输出test函数指针类型
    17. cout << typeid(decltype(test(0))).name() << endl;//注意:这里并不会调用test函数只是推断test函数的返回值类型
    18. func(10);//test函数指针实例化对象
    19. return 0;
    20. }

    运行我们来看结果:

    2.1 decltype返回值类型追踪

    我们先来看一个场景:

     上面的场景我们发现在模板函数中返回值类型是难以确认的因为在调用的时候传入参数不同那么返回值也相应的不同,所以我们就想如果返回值可以根据结果来自动检测类型从而返回那么该多好,这里就用到了decltype返回值追踪

    1. #include<iostream>
    2. //decltype返回值追踪
    3. template <class T1, class T2>
    4. //auto是起到占位符的作用因为返回值不可以不给
    5. auto add(T1 left, T2 right)->decltype(left+right)
    6. {
    7. return left + right;
    8. }
    9. int main()
    10. {
    11. cout << typeid(decltype(add(1, 1.2))).name() << endl;;
    12. return 0;
    13. }

    运行一下:可以看到这时我们的返回值自动识别为double类型

     

    就像如下这种写法:

    我们可以看到直接报错了,这是因为编译器编译代码从左到右编译那么,这里将decltype(left+right) 写到返回值处,这时编译器就懵逼了left和right是个啥东西因为这里还没有编译到left和right所以我们一般都是写道函数名后面,函数的返回值用占位符auto来表示。

    看到这里如果觉得有用的话不如点个赞吧!!! 

     

  • 相关阅读:
    EPPlus 6.1.0 行走在2022.11
    我的创业之路:日记一则
    想要避免After Effects渲染失败的问题,5个小技巧必看
    FFmpeg 中 Filters 使用文档介绍
    蓝桥杯 题库 简单 每日十题 day12
    支持向量机之线性可分向量机
    有什么好的开源自动化测试框架可以推荐?
    Splashtop 远程控制软件即将推出的新功能
    eve-ng导入华为路由器镜像
    VLAN多变一的业务场景解答
  • 原文地址:https://blog.csdn.net/weixin_45897952/article/details/125589306