auto与decltype区别
来源:互联网 发布:days软件 编辑:程序博客网 时间:2024/05/16 17:03
<span style="font-size:18px;">//const 与auto和decltypeconst int a(4);auto aa(a);//int const auto aa2(a);//const int 可以通过加上const使得aa2被顶层const修饰decltype(a) ad (4);//const intconst decltype(a) ad2(4);//const 也可以加但是多余const int*a1(&a);auto a1a(a1);//const int * auto能够识别底层constdecltype(a1) a1d;//const int * //constexpr 与auto和decltypeconstexpr int b(4);auto ba(b);//intdecltype(b) bd (4);//const int//bd并不解释为decltype int 类型而是 const int类型。这正说明decltype是const一种特殊情况// &与auto和decltypeconst int &d(4);struct my2 {auto da(d);//auto变量不能放到结构体里面};auto da(d);//int 通过表达式结果的类型推断struct my {decltype(d) dd;};cout << sizeof(my) << endl;//4decltype(d) dd(4);//const int & decltype与表达式的类型密切相关 //[] 与auto和decltypeint e[5];auto ea(e);//int * 编译器最终将[]解析成指针同时auto又是通过表达式结果的类型推断decltype(e) ed;//int ed[5] //() 与auto和decltypeint f();auto fa(f);//int (*fa)()decltype(f) fd;//int fd();</span>
由上述代码可总结出 区别:
auto是通过初始值表达式的返回结果推断其类型,也可以说是编译后的类型,所以对于初始值表达式为数组 引用 顶层const函数 等通过auto得到的类型出入比较大。
Decltype推断对象的类型与表达式形式密切相关。所以所以对于初始值表达式为数组 引用 顶层const函数 等通过decltype完全一致。特别的对于表达式的结果为左值时(包括 * 、前置++ -- 、 (表达式)),推断出引用类型。
通过这些区别,我们可以得到他们的应用:
<span style="font-size:18px;"> int a(4);auto aa(a);//拷贝一个变量的类型和值decltype(a) ad(a);//拷贝一个变量的类型和值int array[5];decltype(array) c;//创建一个同样的数组,在不知道array元素个数的情况下int ra[10][2];decltype (ra) rac;//完美拷贝其类型int(*f[2])();decltype (f) fc;</span>
0 0
- auto与decltype区别
- auto与 decltype 的区别
- C++ auto与decltype的区别
- auto 与decltype
- auto 与 decltype
- auto与decltype
- auto与decltype
- auto 和 decltype的区别
- c++11新特性---auto 与 decltype
- C++11的auto与decltype
- auto 与decltype 两种类型推断
- auto和decltype的用法总结以及主要区别
- C++11 理解 (一) 之 auto与decltype的运用
- C++11中的新关键字:auto与decltype
- c++:自动获取数据类型,auto与decltype的异同
- [c++ primer笔记][c++11] auto与decltype
- C++ 11 auto & decltype
- C++ 11 auto & decltype
- 第十一周--数据结构--哈夫曼编码的算法验证
- linux设备驱动归纳总结(七):2.内核定时器
- ionic tabs置顶
- C++primer第五版学习过程 第一章例题
- 第十三周项目4Floyd算法验证
- auto与decltype区别
- MVP
- 第十三周项目5—拓扑排序算法验证
- DOS order
- hdu 5569 matrix(dp)
- 十二周 项目四 利用遍历思想求解图问题(2)
- UCS2编码与解码
- angularJS学习之路(十六)---ng-class
- 浅析java中Map,List与Set