C++11 自动推导auto
C++11 自动推导auto
C++11中引入的auto主要有两种用途:自动类型推导和返回值占位。
auto在C++98中的标识临时变量的语义,由于使用极少且多余,在C++11中已被删除。前后两个标准的auto,完全是两个概念。
自动类型推导
auto的自动类型推导,用于从初始化表达式中推断出变量的数据类型。通过auto的自动类型推导,可以大大简化我们的编程工作。
auto实际上实在编译时对变量进行了类型推导,所以不会对程序的运行效率造成不良影响。另外,auto并不会影响编译速度,因为编译时本来也要右侧推导然后判断与左侧是否匹配。
#define _CRT_SECURE_NO_WARNINGS #include <iostream>
#include <string>
#include <vector>
#include <map> // 3. 使用模板技术时,如果某个变量的类型依赖于模板参数,不使用auto将很难确定变量的类型
template <typename T, typename U>
void Multiply(T t, U u)
{
auto v = t * u; // 使用auto后,将由编译器自动进行确定
} class student
{
public:
static int var1;
//auto var2; 错误,非静态成员变量
//static auto var3; 错误,需要初始值
}; int student::var1 = ; //void fun(auto x = 1) {} 错误,auto函数参数,有些编译器无法通过编译 void mytest()
{
//auto a; 错误,没有初始化表达式,无法推断出a的类型
//auto int a1 = 0; 错误,auto临时变量的语义在C++11中已不存在, 这是旧标准的用法。 // 1. 自动帮助推导类型
auto a = ; // a ---> int
auto c = 'A'; // c ---> char
auto s("hello"); // s --> const char *
// 2. 类型冗长
std::map<int, std::map<int, int> > map_;
std::map<int, std::map<int, int> >::const_iterator itr1 = map_.begin();
const auto itr2 = map_.begin();
auto ptr = []() // ptr ---> void ptr()
{
std::cout << "mytest ..." << std::endl;
}; // lambda 表达式 char x[];
auto y = x; // y ---> char * // auto会退化成指向数组的指针,除非被声明为引用
// auto z[3] = x; 错误,auto数组,无法通过编译 return;
} int main()
{
mytest(); system("pause");
return ;
}
2.使用注意事项
1、我们可以使用valatile,pointer(*),reference(&),rvalue reference(&&) 来修饰auto
auto k = 5;
auto* pK = new auto(k);
auto** ppK = new auto(&k);
const auto n = 6;
2、用auto声明的变量必须初始化
auto m; // m should be intialized
3、auto不能与其他类型组合连用
auto int p; // 这是旧auto的做法。
4、函数和模板参数不能被声明为auto
void MyFunction(auto parameter){} // no auto as method argument
template<auto T> // utter nonsense - not allowed
void Fun(T t){}
5、定义在堆上的变量,使用了auto的表达式必须被初始化
int* p = new auto(0); //fine
int* pp = new auto(); // should be initialized
auto x = new auto(); // 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)
6、以为auto是一个占位符,并不是一个他自己的类型,因此不能用于类型转换或其他一些操作,如sizeof和typeid
int value = 123;
auto x2 = (auto)value; // no casting using auto
auto x3 = static_cast<auto>(value); // same as above
7、定义在一个auto序列的变量必须始终推导成同一类型
auto x1 = 5, x2 = 5.0, x3='r'; // This is too much....we cannot combine like this
8、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
9、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
在C++11之前,auto关键字用来指定存储期.在新标准中,它的功能变为类型推断.auto现在成了一个类型的占位符,通知编译器去根据初始化代码推断所声明变量的真实类型.使用auto会拖慢c++效率吗? ...
- C++ 11 学习1:类型自动推导 auto和decltype
Cocos 3.x 用了大量的C++ 11 的东西,所以作为一个C++忠实粉丝,有必要对C++ 11进行一个系统的学习. 使用C++11之前,一定要注意自己使用的编译器对C++11的支持情况,有些编译 ...
- C++11 - 类型推导auto关键字
在C++11中,auto关键字被作为类型自动类型推导关键字 (1)基本用法 C++98:类型 变量名 = 初值; int i = 10; C++11:auto 变量名 = 初值; auto i ...
- C++11的auto自动推导类型
auto是C++11的类型推导关键字,很强大 例程看一下它的用法 #include<vector> #include<algorithm> #include<functi ...
- C++11特性:auto关键字
前言 本文的内容已经不新鲜了.关于auto,翻来覆去被人知道的都是这些东西,本文并没有提出新颖的auto用法. 本人原是痛恨博客一篇篇都是copy而来缺乏新意的探索,当然,本文不是copy而来,但发布 ...
- C++11:使用 auto/decltype/result_of使代码可读易维护
C++11 终于加入了自动类型推导.以前,我们不得不使用Boost的相关组件来实现,现在,我们可以使用"原生态"的自动类型推导了! C++引入自动的类型推导,并不是在向动态语言(强 ...
- C++17尝鲜:类模板中的模板参数自动推导
模板参数自动推导 在C++17之前,类模板构造器的模板参数是不能像函数模板的模板参数那样被自动推导的,比如我们无法写 std::pair a{1, "a"s}; // C++17 ...
- C++隐式推导-auto关键词
总述 C++中有一个关键字,它不进行显式声明,而进行隐式推导,auto可以在声明变量时根据变量初始值的类型自动为此变量选择匹配的类型.C++语言类似的关键字还有decltype. 如何评价 C++ 1 ...
- C++11类型推导
[C++11类型推导] auto 关键字.这会依据该初始化子(initializer)的具体类型产生参数: 除此之外,decltype 能够被用来在编译期决定一个表示式的类型. 参考:http://z ...
随机推荐
- jquery mouseover与mouseenter区别
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- 20155223 Exp6 信息收集与漏洞扫描
20155223 Exp6 信息收集与漏洞扫描 本次实验以熟悉信息收集手段与漏洞扫描手段为主. 实践步骤 whois域名查找 在虚拟机Kali的终端输入命令:whois baidu.com,查询百度的 ...
- CISCN 应用环境相关指令备忘录
1 - 关于Python环境的 使用Anaconda2管理Python环境 1.1 - 安装 官网下载安装包下载. 1.2 - 创建Python环境 localhost:template mac$ c ...
- 20155331《网络对抗》Exp7 网络欺诈防范
20155331<网络对抗>Exp7 网络欺诈防范 实验内容 本实践的目标理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法.具体实践有: 简单应用SET工具建立冒名网站 et ...
- 【转】CentOS 5 上安装git
转自 http://www.cnblogs.com/Neddy/archive/2011/02/28/1967548.html 注意安装的时候 都要以root身份 //先安装git依赖的包 yum i ...
- Elasticsearch Query DSL 整理总结(一)—— Query DSL 概要,MatchAllQuery,全文查询简述
目录 引言 概要 Query and filter context Match All Query 全文查询 Full text queries 小结 参考文档 引言 虽然之前做过 elasticse ...
- R实战 第八篇:重塑数据(reshape2)
数据重塑通常使用reshape2包,reshape2包用于实现对宽数据及长数据之间的相互转换,由于reshape2包不在R的默认安装包列表中,在第一次使用之前,需要安装和引用: install.pac ...
- C# Language Specification 5.0 (翻译)第二章 词法结构
程序 C# 程序(program)由至少一个源文件(source files)组成,其正式称谓为编译单元(compilation units)[1].每个源文件都是有序的 Unicode 字符序列.源 ...
- 并发编程(Concurrent programming)
并发编程(Concurrent programming) 1.并发编程概述 2.委托(delegate) 3.事件(event) 4.线程(thread) 5.线程池(threadPool) 6.任务 ...
- storm从入门到放弃(三),放弃使用 StreamId 特性
序:StreamId是storm中实现DAG有向无环图的重要一个特性,但是从实际生产环境来看,这个功能其实蛮影响生产环境的稳定性的,我们系统在迭代时会带来整体服务的不可用. StreamId是stor ...