C++11 auto and decltype
1、auto关键字
C++新标准引入auto关键词,此auto与之前C语言的auto意义已经不一样了。
这里的auto是修饰未知变量的类型,编译器会通过此变量的初始化自动推导变量的类型。
例如:auto i = 0 ;编译器会通过“0”值,推导出变量i是整型。
如果初始值是引用,如:
- int i = 4;
- int &ri = i;
- auto ai = ri;
通过应用是利用了ri指向的对象,所以ai的类型是int。也就是ai与i的类型是相同的。
另外,auto是忽略top-level const,而保留low-level const属性。具体说明如下:
- const int ci = i, &cr = ci;
- auto b = ci; // b is an int (top-level const in ci is dropped)
- auto c = cr; // c is an int (cr is an alias for ci whose const is top-level)
- auto d = &i; // d is an int * (& of an int object is int *)
- auto e = &ci; // e is const int * (& of a const object is low-level const)
上面的变量b、c、d、e说明了auto的一个特别属性,该特性与下面将要介绍的另一个关键字decltype不同。
为了实现top-level const,需要在auto前面加const,也就是const auto f = ci,那么f就是const int 类型。
2、decltype
关键字decltype能够推导表达式或者函数的返回类型,但不对表达式求值。
例如:
- decltype(f()) sum = x;
变量sum拥有f()的返回值类型。与auto不同,decltype能够返回变量的top-level属性,如果变量是引用,那么decltype修饰的变量也是引用。例如:
- const int ci = 0, &cj = ci;
- decltype(ci) x = 0; // x has type const int
- decltype(cj) y = x; // y has type const int & and is bound to x
- decltype(cj) z; // error: z is a reference and must be initialized.
当表达式不是变量,而是可作为左值的对象时,那么decltype返回的时指向该对象类型的应用。
- int *p = &i;
- decltype(*p) pri = i;
- decltype(p) pi = &i;
其中pri是int &类型,而pi是int *类型。
decltype的推导结果还与给定的表达式的形式有关。如果对变量求类型,那么decltype直接返回变量的类型;如果变量加括号后,那么decltype返回的类型是引用,引用的类型就是该变量的类型。
- decltype(i) e; // e is an int variable uninitialized
- decltype((i)) d; // error: d is int & and must be initialized
C++11 auto and decltype的更多相关文章
- c++11 auto 与 decltype 详解
转自: here 一. auto简介 编程时候常常需要把表达式的值付给变量,需要在声明变量的时候清楚的知道变量是什么类型.然而做到这一点并非那么容易(特别是模板中),有时候根本做不到.为了解决这个问题 ...
- C++11 auto和decltype推导规则
VS2015下测试: decltype: class Foo {}; int &func_int_r(void) { int i = 0; return i; }; int && ...
- C++ 11 学习1:类型自动推导 auto和decltype
Cocos 3.x 用了大量的C++ 11 的东西,所以作为一个C++忠实粉丝,有必要对C++ 11进行一个系统的学习. 使用C++11之前,一定要注意自己使用的编译器对C++11的支持情况,有些编译 ...
- c++11——auto,decltype类型推导
c++11中引入了auto和decltype关键字实现类型推导,通过这两个关键字不仅能够方便的获取复杂的类型,而且还能简化书写,提高编码效率. auto和decltype的类型推导都是编译器在 ...
- 【C++】C++11的auto和decltype关键字
转自: http://www.linuxidc.com/Linux/2015-02/113568.htm 今天要介绍C++11中两个重要的关键字,即auto和decltype.实际上在C++98中,已 ...
- C++11新特性— auto 和 decltype 区别和联系
一. auto简介 编程时候常常需要把表达式的值付给变量,需要在声明变量的时候清楚的知道变量是什么类型.然而做到这一点并非那么容易(特别是模板中),有时候根本做不到.为了解决这个问题,C++11新标准 ...
- C++ 11 新特性: auto 和 decltype 区别和联系
一. auto简介编程时候常常需要把表达式的值付给变量,需要在声明变量的时候清楚的知道变量是什么类型.然而做到这一点并非那么容易(特别是模板中),有时候根本做不到.为了解决这个问题,C++11新标准就 ...
- 关于auto和decltype
auto会忽略顶层const,保留底层const ; const int* const p = &i; auto p2 = p; //p2是const int*,不是const int* co ...
- C++11新特性--decltype (转)
返回值 decltype(表达式) [返回值的类型是表达式参数的类型] 这个可也用来决定表达式的类型,就像Bjarne暗示的一样,如果我们需要去初始化某种类型的变量,auto是最简单的选择,但是如果我 ...
随机推荐
- Computer Vision: OpenCV, Feature Tracking, and Beyond--From <<Make Things See>> by Greg
In the 1960s, the legendary Stanford artificial intelligence pioneer, John McCarthy, famously gave a ...
- Android 图片滤镜工具——高斯模糊
===================高斯模糊========================= 创建一个 ImageFilter 类(滤镜工具),代码如下: import android.graph ...
- IIS相关知识
1.在web.config中,iis6使用<system.web>下配置项,iis7使用<system.webServer>下配置项 2.<httpHandlers> ...
- 初学Redis(1)——认识Redis
Redis官网对Redis的定义是:“Redis is an open source, BSD licensed, advanced key-value cache and store”,可以看出,R ...
- mysql状态取反(0变1,1变0)
如果要改变status状态 update table_name status=ABS(status-1); //取绝对值 则0-->1 1-->0
- HTTP协议GET和POST请求的区别
浏览器中输入网址访问资源一般都是通过GET方式:在FORM提交中,可以通过Method指定提交方式为GET或者POST,默认为GET提交.Http协议定义了与服务器交互的不同方法,最基本的方法有4种, ...
- Oracle查询实用命令
1.设置每行的长度: SET LIN[ESIZE] 200; 2.设置分页数量: SET PAGES[IZE] 50; 3.查看表空间相关信息: select file_id, tablespace_ ...
- [译]:Orchard入门——部件管理
原文链接:Managing Widgets 在Orchard中,部件是可以加入到当前当前主题任何位置或区域(如侧栏sidebar或底部区域footer)的UI块(如:HTML)或代码部分(如:内容部分 ...
- 关于mat2gray
最小的是0,最大的是1,那么介于中间的那些值我们怎么处理? 那么事实上我们试了很多样例之后.. 我猜他是..每个步长step_length=1/(max-min+1) 然后每个值就会变成(val-1) ...
- [leetcode] 题型整理之排序
75. Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects ...