【C++11】新特性 之 auto的使用
C++11中引入的auto主要有两种用途:自己主动类型判断和返回值占位。auto在C++98中的标识暂时变量的语义,因为使用极少且多余。在C++11中已被删除。前后两个标准的auto,全然是两个概念。
1. 自己主动类型判断
auto自己主动类型判断。用于从初始化表达式中判断出变量的数据类型。通过auto的自己主动类型判断,能够大大简化我们的编程工作。以下是一些使用auto的样例。
- #include <vector>
- #include <map>
- using namespace std;
- int main(int argc, char *argv[], char *env[])
- {
- // auto a; // 错误,没有初始化表达式,无法判断出a的类型
- // auto int a = 10; // 错误,auto暂时变量的语义在C++11中已不存在, 这是旧标准的使用方法。
- // 1. 自己主动帮助推导类型
- auto a = 10;
- auto c = 'A';
- auto s("hello");
- // 2. 类型冗长
- map<int, map<int,int> > map_;
- map<int, map<int,int>>::const_iterator itr1 = map_.begin();
- const auto itr2 = map_.begin();
- auto ptr = []()
- {
- std::cout << "hello world" << std::endl;
- };
- return 0;
- };
- // 3. 使用模板技术时。假设某个变量的类型依赖于模板參数,
- // 不使用auto将非常难确定变量的类型(使用auto后,将由编译器自己主动进行确定)。
- template <class T, class U>
- void Multiply(T t, U u)
- {
- auto v = t * u;
- }
2. 返回值占位
- template <typename T1, typename T2>
- auto compose(T1 t1, T2 t2) -> decltype(t1 + t2)
- {
- return t1+t2;
- }
- auto v = compose(2, 3.14); // v's type is double
3.使用注意事项
①我们能够使用valatile,pointer(*)。reference(&),rvalue reference(&&) 来修饰auto
- auto k = 5;
- auto* pK = new auto(k);
- auto** ppK = new auto(&k);
- const auto n = 6;
②用auto声明的变量必须初始化
- auto m; // m should be intialized
③auto不能与其它类型组合连用
- auto int p; // 这是旧auto的做法。
④函数和模板參数不能被声明为auto
- void MyFunction(auto parameter){} // no auto as method argument
- template<auto T> // utter nonsense - not allowed
- void Fun(T t){}
⑤定义在堆上的变量,使用了auto的表达式必须被初始化
- int* p = new auto(0); //fine
- int* pp = new auto(); // should be initialized
- auto x = new auto(); // Hmmm ... no intializer
- auto* y = new auto(9); // Fine. Here y is a int*
- auto z = new auto(9); //Fine. Here z is a int* (It is not just an int)
⑥以为auto是一个占位符,并非一个他自己的类型,因此不能用于类型转换或其它一些操作,如sizeof和typeid
- int value = 123;
- auto x2 = (auto)value; // no casting using auto
- auto x3 = static_cast<auto>(value); // same as above
⑦定义在一个auto序列的变量必须始终推导成同一类型
- auto x1 = 5, x2 = 5.0, x3='r'; // This is too much....we cannot combine like this
⑧auto不能自己主动推导成CV-qualifiers(constant & volatile qualifiers),除非被声明为引用类型
- const int i = 99;
- auto j = i; // j is int, rather than const int
- j = 100 // Fine. As j is not constant
- // Now let us try to have reference
- auto& k = i; // Now k is const int&
- k = 100; // Error. k is constant
- // Similarly with volatile qualifer
⑨auto会退化成指向数组的指针,除非被声明为引用
- int a[9];
- auto j = a;
- cout<<typeid(j).name()<<endl; // This will print int*
- auto& k = a;
- cout<<typeid(k).name()<<endl; // This will print int [9]
【C++11】新特性 之 auto的使用的更多相关文章
- C++ 11 新特性: auto 和 decltype 区别和联系
一. auto简介编程时候常常需要把表达式的值付给变量,需要在声明变量的时候清楚的知道变量是什么类型.然而做到这一点并非那么容易(特别是模板中),有时候根本做不到.为了解决这个问题,C++11新标准就 ...
- C++11新特性之三——auto
C++11中引入的auto主要有两种用途:自动类型推断和返回值占位.auto在C++98中的标识临时变量的语义,由于使用极少且多余,在C++11中已被删除.前后两个标准的auto,完全是两个概念 1. ...
- C++11新特性之auto
auto的使用 c++11引入了auto类型说明符,auto让编译器通过初始值来推算变量的类型,所以auto定义的变量必须有初始值. 使用auto也能在一条语句中声明多个变量,因为一条声明语句只能 ...
- 【C++11新特性】 auto关键字
原文链接: http://blog.csdn.net/xiejingfa/article/details/50469045 熟悉脚本语言的人都知道,很多脚本语言都引入了“类型自动推断”技术:比如pyt ...
- C++11新特性— auto 和 decltype 区别和联系
一. auto简介 编程时候常常需要把表达式的值付给变量,需要在声明变量的时候清楚的知道变量是什么类型.然而做到这一点并非那么容易(特别是模板中),有时候根本做不到.为了解决这个问题,C++11新标准 ...
- C++11新特性总结 (二)
1. 范围for语句 C++11 引入了一种更为简单的for语句,这种for语句可以很方便的遍历容器或其他序列的所有元素 vector<int> vec = {1,2,3,4,5,6}; ...
- C++11新特性总结 (一)
1. 概述 最近在看C++ Primer5 刚好看到一半,总结一下C++11里面确实加了很多新东西,如果没有任何了解,别说自己写了,看别人写的代码估计都会有些吃力.C++ Primer5是学习C++1 ...
- C++ 11 新特性
C++11新特性: 1.auto 2.nullptr 3.for 4.lambda表达式 5.override ...
- [转载] C++11新特性
C++11标准发布已有一段时间了, 维基百科上有对C++11新标准的变化和C++11新特性介绍的文章. 我是一名C++程序员,非常想了解一下C++11. 英文版的维基百科看起来非常费劲,而中文版维基百 ...
- 在C++98基础上学习C++11新特性
自己一直用的是C++98规范来编程,对于C++11只闻其名却没用过其特性.近期因为工作的需要,需要掌握C++11的一些特性,所以查阅了一些C++11资料.因为自己有C++98的基础,所以从C++98过 ...
随机推荐
- echarts 饼状图 改变折线长度
$(function (){ //ups部分 var myChart = echarts.init(document.getElementById('result')) var option = { ...
- c++编译时打印宏定义
#pragma message("this is message") #pragma message只能打印字符串,如果想打印任何宏定义可使用: #define PRINT_MAC ...
- STM32示波器 信号发生器
源: STM32示波器 信号发生器
- bzoj1605 / P2905 [USACO08OPEN]农场危机Crisis on the Farm
P2905 [USACO08OPEN]农场危机Crisis on the Farm 发现总步数$k<=30$,考虑用$k$瞎搞 设$f[u][i][j]$表示已经吹$u$次哨,全体奶牛向右走$i ...
- 数据导入(二):MapReduce
package test091201; import java.io.IOException; import java.text.SimpleDateFormat; import java.util. ...
- 图像添加logo水印函数
<?php //图像添加水印函数 /** *为一张图片添加上一个logo水印(以保存新图片的方式实现) *@param string $picname 被缩放的处理图片源 *@param int ...
- 使用javascript模拟常见数据结构(一)
数据结构和算法可算是每个程序员的必备技能,而随着前端工作的深入,对于数据结构的知识真的是越来越需要掌握了.好了,于是乎最近看了<javascript数据结构和算法>,算是对于后面的使用C语 ...
- python 正则匹配字符串里面的字符
import re x=re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest') print(x)
- 【整理】STL中的bitset(二进制华丽解决假五维偏序题)
------------更多Bitset的运用,请看这里http://www.cnblogs.com/hua-dong/p/8519739.html. 由于在学cdq分治,看到了这道题.先来看一道题目 ...
- SNMP:使用net-snmp捕捉trap
管理端:172.18.0.135 win7系统 代理端:172.18.0.212 Debian7.2 前提:代理端已配置snmp,可正常实现用SNMP协议实现系统信息监控 1.管理端下 ...