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的更多相关文章

  1. C++11 类型推导auto

    在C++11之前,auto关键字用来指定存储期.在新标准中,它的功能变为类型推断.auto现在成了一个类型的占位符,通知编译器去根据初始化代码推断所声明变量的真实类型.使用auto会拖慢c++效率吗? ...

  2. C++ 11 学习1:类型自动推导 auto和decltype

    Cocos 3.x 用了大量的C++ 11 的东西,所以作为一个C++忠实粉丝,有必要对C++ 11进行一个系统的学习. 使用C++11之前,一定要注意自己使用的编译器对C++11的支持情况,有些编译 ...

  3. C++11 - 类型推导auto关键字

    在C++11中,auto关键字被作为类型自动类型推导关键字 (1)基本用法 C++98:类型 变量名 = 初值;   int i = 10; C++11:auto 变量名 = 初值;  auto i ...

  4. C++11的auto自动推导类型

    auto是C++11的类型推导关键字,很强大 例程看一下它的用法 #include<vector> #include<algorithm> #include<functi ...

  5. C++11特性:auto关键字

    前言 本文的内容已经不新鲜了.关于auto,翻来覆去被人知道的都是这些东西,本文并没有提出新颖的auto用法. 本人原是痛恨博客一篇篇都是copy而来缺乏新意的探索,当然,本文不是copy而来,但发布 ...

  6. C++11:使用 auto/decltype/result_of使代码可读易维护

    C++11 终于加入了自动类型推导.以前,我们不得不使用Boost的相关组件来实现,现在,我们可以使用"原生态"的自动类型推导了! C++引入自动的类型推导,并不是在向动态语言(强 ...

  7. C++17尝鲜:类模板中的模板参数自动推导

    模板参数自动推导 在C++17之前,类模板构造器的模板参数是不能像函数模板的模板参数那样被自动推导的,比如我们无法写 std::pair a{1, "a"s}; // C++17 ...

  8. C++隐式推导-auto关键词

    总述 C++中有一个关键字,它不进行显式声明,而进行隐式推导,auto可以在声明变量时根据变量初始值的类型自动为此变量选择匹配的类型.C++语言类似的关键字还有decltype. 如何评价 C++ 1 ...

  9. C++11类型推导

    [C++11类型推导] auto 关键字.这会依据该初始化子(initializer)的具体类型产生参数: 除此之外,decltype 能够被用来在编译期决定一个表示式的类型. 参考:http://z ...

随机推荐

  1. static成员函数不能调用non-static成员函数

    1 一般类静态成员函数不能调用非静态成员函数 2 static成员函数可以调用构造函数吗? 答案是肯定的,由于static成员函数没有this指针,所以一般static成员函数是不能访问non-sta ...

  2. 笔记:UITextView内容垂直居中方法

    - (void)contentSizeToFit { //先判断一下有没有文字(没文字就没必要设置居中了) ) { //textView的contentSize属性 CGSize contentSiz ...

  3. 大数据入门第十二天——azkaban入门

    一.概述 1.azkaban是什么 通过官方文档:https://azkaban.github.io/ Azkaban is a batch workflow job scheduler create ...

  4. 汇编 fsub ,fmul,fdiv,fild,CVTTPS2PI 指令

    知识点:  浮点指令 fsub 一.浮点指令fsub 格式 fsub memvar // st0=st0-memvar 知识点:  浮点指令 fmul 一.浮点指令fmul 格式 fmul mem ...

  5. Selenium-ActionChainsApi接口详解

    ActionChains 有时候我们在通过Selenium做UI自动化的时候,明明能够在DOM树内看到这个元素,但是我在通过driver click.sendkey的时候,就是点击不到或无法输入字符串 ...

  6. SQL 追踪

    SQL追踪(SQL Trace)是一个轻量级的追踪工具,按照事件(Events)记录数据库发生的消息,几乎对数据库性能没有什么影响.SQL Server内置一个Trace,称作默认追踪(Default ...

  7. 基于spring的redisTemplate的缓存工具类

    pom.xml文件添加 <!-- config redis data and client jar --><dependency> <groupId>org.spr ...

  8. stl源码剖析 详细学习笔记 set map

    // //  set map.cpp //  笔记 // //  Created by fam on 15/3/23. // // //---------------------------15/03 ...

  9. gulp.src()内部实现探究

    写在前面 本来是想写个如何编写gulp插件的科普文的,突然探究欲又发作了,于是就有了这篇东西...翻了下源码看了下gulp.src()的实现,不禁由衷感慨:肿么这么复杂... 进入正题 首先我们看下g ...

  10. 异步编程之asyncio简单介绍

    引言: python由于GIL(全局锁)的存在,不能发挥多核的优势,其性能一直饱受诟病.然而在IO密集型的网络编程里,异步处理比同步处理能提升成百上千倍的效率,弥补了python性能方面的短板. as ...