【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怎样实现回调函数. ...
随机推荐
- Extjs 使用图标字体来美化按钮)
1. 使用Font Awesome,下载地址http://www.bootcss.com/p/font-awesome/#icons-new 2. 把font和css目录放到 Ext的app目录下面 ...
- nginx安装(1) – ttlsa教程系列之nginx
1.必要软件准备 安装pcre 为了支持rewrite功能,我们需要安装pcre 1 # yum install pcre* //如过你已经装了,请跳过这一步 安装openssl 需要ssl的支持 ...
- poj3013 邻接表+优先队列+Dij
把我坑到死的题 开始开题以为是全图连通是的最小值 ,以为是最小生成树,然后敲了发现不是,看了下别人的题意,然后懂了: 然后发现数据大,要用邻接表就去学了一下邻接表,然后又去学了下优先队列优化的dij: ...
- 10 函数的复写-override
1.函数的复写:override 2.使用super调用父类的成员函数 class Person { String name; int age; void introduce() { System.o ...
- [转]Ajax跨域请求
一.编一个服务器端servlet @RequestMapping("/haha") @ResponseBody String haha(String haha, HttpServl ...
- c# 读取嵌入式文件
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Reflect ...
- 【BZOJ-3033】太鼓达人 欧拉图 + 暴搜
3033: 太鼓达人 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 204 Solved: 154[Submit][Status][Discuss] ...
- 【poj1160】 Post Office
http://poj.org/problem?id=1160 (题目链接) 题意 按照递增顺序给出一条直线上坐标互不相同的n个村庄,要求从中选择p个村庄建立邮局,每个村庄使用离它最近的那个邮局,使得所 ...
- codeforces 21D:Traveling Graph
Description You are given undirected weighted graph. Find the length of the shortest cycle which sta ...
- POJ1061 青蛙的约会
Description 两 只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它 们出发之前忘记了一件很重要 ...