【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新特性总结 (二)
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过 ...
- C++11新特性——range for
很多编程语言都有range for语法功能,自C++11起,终于将这个重要功能加入C++标准中.range for语句,可以方便的遍历给定序列中的每个元素并对其执行某种操作. 1.基本语法 for(d ...
- C++11新特性之一——Lambda表达式
C++11新特性总结可以参考:http://www.cnblogs.com/pzhfei/archive/2013/03/02/CPP_new_feature.html#section_6.8 C++ ...
- C++11新特性应用--实现延时求值(std::function和std::bind)
说是延时求值,注意还是想搞一搞std::function和std::bind. 之前博客<C++11新特性之std::function>注意是std::function怎样实现回调函数. ...
随机推荐
- Android判断当前系统时间是否在指定时间的范围内(免消息打扰)
/** * 判断当前系统时间是否在指定时间的范围内 * * @param beginHour * 开始小时,例如22 * @param beginMin * 开始小时的分钟数,例如30 * @para ...
- iOS开发-- 利用AVPlayer播放远程音乐和视频
一.简单的播放音乐和视频,播放视频的工具栏需要自己写 二.利用老师封装的框架实现视频播放 链接:http://pan.baidu.com/s/1hrEKlus 密码:8e7g
- Image Segmentation的定义
Definition 图像分割将一张图分为\(n\)个region, 需要满足下面5个条件 每一个像素都要属于一个region 每个region都是连通的 region与region之间没有交集 re ...
- 51nod 1441 欧拉筛法
1441 士兵的数字游戏 题目来源: CodeForces 基准时间限制:6 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 两个士兵正在玩一个游戏,游戏开始的时候, ...
- java-首字母大小写
/** * 首字母小写 * * @param str * @return */ public static String toLowerCaseFirstChar(String s) { if (Ch ...
- iOS之类的本质
1.本质 类的本质其实也是一个对象(类对象) 程序中第一次使用该类的时候被创建,在整个程序中只有一份. 此后每次使用都是这个类对象,它在程序运行时一直存在. 类对象是一种数据结构,存储类的基本信息:类 ...
- 【BZOJ-1367】sequence 可并堆+中位数
1367: [Baltic2004]sequence Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 932 Solved: 348[Submit][S ...
- Hadoop中JAVA不经过Catch(Exception e)直接到finally或者退出原因
原来是被变成Throwable抛出来了!而Exception是Throwable的子类,所以无法捕捉到,只有捕捉Throwable的时候,才可以将错误信息打印!
- ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet
严重: Context initialization failedorg.springframework.beans.factory.BeanDefinitionStoreException: Fai ...
- bzoj3283: 运算器
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...