【校招面试 之 C/C++】第30题 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]
转自:https://blog.csdn.net/huang_xw/article/details/8760403
【校招面试 之 C/C++】第30题 C++ 11新特性(一)之auto关键字的更多相关文章
- 【校招面试 之 C/C++】第33题 C++ 11新特性(四)之STL容器
C++ 11新增array.forward_list(单链表).unordered_set.unordered_map集中容器.
- 【校招面试 之 C/C++】第32题 C++ 11新特性(三)之for关键字
1.for循环的一般写法: int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for (int i = 0; i < 10; i++) cout ...
- 【校招面试 之 C/C++】第31题 C++ 11新特性(二)之nullptr关键字
1. 引入nullptr的原因 引入nullptr的原因,这个要从NULL说起.对于C和C++程序员来说,一定不会对NULL感到陌生.但是C和C++中的NULL却不等价.NULL表示指针不指向任何对象 ...
- 【校招面试 之 网络】第3题 HTTP请求行、请求头、请求体详解
1.HTTP请求报文解剖 HTTP请求报文由3部分组成(请求行+请求头+请求体): 下面是一个实际的请求报文: ①是请求方法,GET和POST是最常见的HTTP方法,除此以外还包括DELETE.HEA ...
- 【校招面试 之 网络】第2题 TCP的可靠传输、流量控制、滑动窗口
1.可靠传输 (1)三次握手 TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接: (1)第一次握手:建立连接时,客户端A发送SYN包(SYN=j)到服务器B,并进入SYN_S ...
- 【校招面试 之 网络】第1题 TCP和UDP
TCP UDP1.TCP与UDP基本区别 (1)基于连接与无连接 (2)TCP要求系统资源较多,UDP较少: (3)UDP程序结构较简单(头只有8个字节:源端口号.目标端口号.长度.差错) ...
- 前端面试经典题之ES6新特性
ES6 主要是为了解决 ES5 的先天不足,在原先ES5的基础上新增了许多内容,本篇文章将列举出ES6中新增的10大特性. 一. let 和 const 与var不同,let和const都是用于命名局 ...
- 【C++11】30分钟了解C++11新特性
作者:王选易,出处:http://www.cnblogs.com/neverdie/ 欢迎转载,也请保留这段声明.如果你喜欢这篇文章,请点[推荐].谢谢! 什么是C++11 C++11是曾经被叫做C+ ...
- 一次电话Java面试的问题总结(JDK8新特性、哈希冲突、HashMap原理、线程安全、Linux查询命令、Hadoop节点)
面试涉及问题含有: Java JDK8新特性 集合(哈希冲突.HashMap的原理.自动排序的集合TreeSet) 多线程安全问题 String和StringBuffer JVM 原理.运行流程.内部 ...
随机推荐
- Apache JMeter配置、安装
一. 工具描述 apache jmeter是100%的java桌面应用程序,它被设计用来加载被测试软件功能特性.度量被测试软件的性能.设计jmeter的初衷是测试web应用,后来又扩充了其它的功能.j ...
- SIP 认证方式
SIP认证是继承了HTTP的认证方式.HTTP的认证方案主要有Basic Authentication Scheme和Digest Access Authentication Scheme两种.而Ba ...
- [datatable]借助DataTable的Compute方法
借助DataTable的Compute方法,DataTable中数据不用事先排好序. 下面代码中的dt是跟前面的是一样的 DataTable dtName = dt.DefaultView.ToTab ...
- python之路之函数03
一 首先我们学到函数的嵌套调用与定义:1 函数嵌套 # def f1(): # print(f1)#我们这里如果输入f1那么输出的则是f1这个变量(函数)所在的地址.如果输入一个字符的话那么就直接输出 ...
- mysql数据库的维护,备份和复制
在数据库运行时维护数据库 执行mysql数据库维护的方法之一就是连接mysql服务器,并告诉它做什么事, 如对myisam数据表进行检查或者修复, 可以使用check table tbname或rep ...
- 3. HashMap和JSONObject用法
<%@page import="net.sf.json.JSONObject"%><%@page import="java.util.List" ...
- 65. sqlserver执行存储过程实例
declare @param varchar(500)exec sp_PUB_GetFlowStatus @ret output,10011,88,1,12print @ret
- java 项目中类找不到异常解决办法
最后点击Apply and Close就可以了
- IdUDPServer中文汉字乱码 及IdTCPClient
官网 http://www.indyproject.org/docsite/html/frames.html?frmname=topic&frmfile=TIdTCPServer_OnExec ...
- foreach 使用&引用赋值要注意的问题
<?php $arr = array('a', 'b', 'c'); $arr2 = array('d', 'e', 'f'); foreach($arr as &$value){ $v ...