- #include<iostream>
- #include <map>
- #include <string>
- using namespace std;
- int main()
- {
- short a = 32670;
- short b = 32670;
-
- // c如果给成short,会造成数据丢失,如果能够让编译器根据a+b的结果推导c的实际类型,就不会存在问题
- short c = a + b;
-
- std::map<std::string, std::string> m{ { "apple", "苹果" }, { "banana", "香蕉" } };
- // 使用迭代器遍历容器, 迭代器类型太繁琐
- std::map<std::string, std::string>::iterator it = m.begin();
- while (it != m.end())
- {
- cout << it->first << " " << it->second << endl;
- ++it;
- }
-
- return 0;
- }
对于上面的这两种情况我们可以用auto和decltype来解决:
- #include<iostream>
- #include<string>
- #include<map>
- using namespace std;
-
- int main()
- {
- short a = 32670;
- short b = 32670;
- auto c = a + b;//这里直接用auto来识别c的类型
- decltype(a + b) d = a + b;//或者用decltype来识别a+b的得到的结果类型,然后来定义c的类型
- cout << typeid(decltype(a + b)).name ()<< endl;
- cout << typeid(c).name() << endl;
- cout << endl;
-
- std::map<std::string, std::string> m{ { "apple", "苹果" }, { "banana", "香蕉" } };
- auto it1 = m.begin();//可以直接用auto来识别迭代器的类型
- while (it1 != m.end())
- {
- cout << it1->first << " " << it1->second << endl;
- ++it1;
- }
- cout << endl;
- decltype(m.begin()) it2=m.begin();
- while (it2 != m.end())
- {
- cout << it2->first << " " << it2->second << endl;
- ++it2;
- }
- return 0;
- }
运行结果:

下面我们来具体介绍auto和decltype
1.对于用auto修饰的变量一定要初始化
- int TestAuto()
- {
- return 10;
- }
- int main()
- {
- int a = 10;
- auto b = a;
- auto c = 'a';
- auto d = TestAuto();
-
- cout << typeid(b).name() << endl;
- cout << typeid(c).name() << endl;
- cout << typeid(d).name() << endl;
-
- //auto e; 无法通过编译,使用auto定义变量时必须对其进行初始化
- return 0;
- }
运行结果:可以看到auto可以自动识别变量的类型

2. 注意auto是不可以作为函数的参数和返回值的
这是因为用auto修饰的变量必须要初始化,这样才能在编译阶段推断出变量的类型,而函数的参数和返回值在程序运行阶段才能确定,所以auto是不可以作为函数的参数和返回值的。
3. auto与指针和引用结合起来使用
用auto声明指针类型时,用auto和auto*没有任何区别,但用auto声明引用类型时则必须加&

4. 在同一行定义多个变量
- void TestAuto()
- {
- auto a = 1, b = 2;
- auto c = 3, d = 4.0; // 该行代码会编译失败,因为c和d的初始化表达式类型不同
- }
5.auto不能用来声明数组

我们在了解了auto的一些基本用法那么我们来解决一下上面我们最一开始提出来的问题:
那么有了auto为什么还要有decltype呢?
这是因为:auto使用的前提是:必须要对auto声明的类型进行初始化,否则编译器无法推导出auto的实际类型。但有时候可能需要根据表达式运行完成之后结果的类型进行推导,因为编译期间,代码不会运行,此时auto也就无能为力。
我们来看一下decltype的常见用法
- #include<iostream>
- using namespace std;
- void* test(int a)
- {
- return new int(a);
- }
- typedef decltype(test)* func;//可以通过推导函数类型来定义函数指针
-
- int main()
- {
- short a = 32670;
- short b = 32670;
- //可以直接通过a+b的结果来推断出d的类型,同时我们发现它可以给用a+b的类型来给多个变量赋值
- //而auto仅仅只能通过变量的初始值给一个变量来确定类型
- decltype(a + b) d = a + b;
- cout << typeid(decltype(test)).name() << endl;//推断test函数的类型
- cout << typeid(func).name() << endl;//输出test函数指针类型
- cout << typeid(decltype(test(0))).name() << endl;//注意:这里并不会调用test函数只是推断test函数的返回值类型
- func(10);//test函数指针实例化对象
- return 0;
- }
运行我们来看结果:
我们先来看一个场景:
上面的场景我们发现在模板函数中返回值类型是难以确认的因为在调用的时候传入参数不同那么返回值也相应的不同,所以我们就想如果返回值可以根据结果来自动检测类型从而返回那么该多好,这里就用到了decltype返回值追踪
- #include<iostream>
- //decltype返回值追踪
- template <class T1, class T2>
- //auto是起到占位符的作用因为返回值不可以不给
- auto add(T1 left, T2 right)->decltype(left+right)
- {
- return left + right;
- }
- int main()
- {
- cout << typeid(decltype(add(1, 1.2))).name() << endl;;
- return 0;
- }
运行一下:可以看到这时我们的返回值自动识别为double类型
就像如下这种写法:
我们可以看到直接报错了,这是因为编译器编译代码从左到右编译那么,这里将decltype(left+right) 写到返回值处,这时编译器就懵逼了left和right是个啥东西因为这里还没有编译到left和right所以我们一般都是写道函数名后面,函数的返回值用占位符auto来表示。
看到这里如果觉得有用的话不如点个赞吧!!! 