Type Deduction 发生在编译时期

可以对一般类型,自定义类型进行类型自推导

下面有两个例子:

1. Using auto with a class

  1. #include <iostream>
  2. #include <typeinfo>
  3.  
  4. using namespace std;
  5.  
  6. class MyClass
  7. {
  8. };
  9.  
  10. int main()
  11. {
  12. auto variable = MyClass();
  13. cout << "Type of variable: " << typeid(variable).name() << endl;
  14. return ;
  15. }

2.Using auto with Uniform Initialization

  1. #include <iostream>
  2. #include <typeinfo>
  3. using namespace std;
  4.  
  5. class MyClass
  6. {
  7. };
  8.  
  9. int main()
  10. {
  11. auto variable{ };
  12. cout << "Type of variable: " << typeid(variable).name() << endl;
  13. auto variable2{ MyClass{} };
  14. cout << "Type of variable: " << typeid(variable2).name() << endl;
  15. return ;
  16. }

use auto for local variables as much as possible. 在局部变量的时候使用auto

2-3. Using Type Deduction的更多相关文章

  1. 现代C++之理解模板类型推断(template type deduction)

    理解模板类型推断(template type deduction) 我们往往不能理解一个复杂的系统是如何运作的,但是却知道这个系统能够做什么.C++的模板类型推断便是如此,把参数传递到模板函数往往能让 ...

  2. [Effective Modern C++] Item 2. Understand auto type deduction - 了解auto类型推断

    条款二 了解auto类型推断 基础知识 除了一处例外,auto的类型推断与template一样.存在一个直接的从template类型推断到auto类型推断的映射 三类情况下的推断如下所示: // ca ...

  3. [Effective Modern C++] Item 1. Understand template type deduction - 了解模板类型推断

    条款一 了解模板类型推断 基本情况 首先定义函数模板和函数调用的形式如下,在编译期间,编译器推断T和ParamType的类型,两者基本不相同,因为ParamType常常包含const.引用等修饰符 t ...

  4. C++ Templates (1.2 模板实参推断 Template Argument Deduction)

    返回完整目录 目录 1.2 模板实参推断 Template Argument Deduction 1.2 模板实参推断 Template Argument Deduction 当调用函数模板(如max ...

  5. Effective Modern C++ 42 Specific Ways to Improve Your Use of C++11 and C++14

    Item 1: Understand template type deduction. Item 2: Understand auto type deduction. Item 3: Understa ...

  6. 2-4. Using auto with Functions

    在C++14中允许使用type deduction用于函数参数和函数返回值 Return Type Deduction in C++11 #include <iostream> using ...

  7. Modern C++ CHAPTER 2(读书笔记)

    CHAPTER 2 Recipe 2-1. Initializing Variables Recipe 2-2. Initializing Objects with Initializer Lists ...

  8. c++ 相关的技术资源整理归类

    最近一段时间 c++ 社区里最火热的话题莫过于 cppcon2015 了, isocpp 上一堆相关的新闻,其中有一个页面罗列了该会议的全部主题, 匆匆一瞥几乎眼花缭乱,为期一个星期的会议竟有上百个演 ...

  9. c++11新特性(了解)

    从C++出来到现在已经13年了. Bjarne Stroustrup(C++的创造者)最近评价C++:”感觉像个新的语言“. 事实上,C++11核心已经发生了很重大的变化: . 支持Lambda表达式 ...

随机推荐

  1. 如何将ASP.NET MVC所有参数均自动设置为默认

    今天看到CSDN上有个问题觉得有点意思:"可不可以ASP.NET MVC所有参数均自动设置为默认" public class HomeController : Controller ...

  2. PVANET----Deep but Lightweight Neural Networks for Real-time Object Detection论文记录

    arxiv上放出的物体检测的文章,在Pascal voc数据集上排第二.源码也已放出(https://github.com/sanghoon/pva-faster-rcnn),又可以慢慢把玩了.这篇文 ...

  3. Ninject之旅之四:Ninject模块

    摘要 随着应用程序的增长,注册的服务列表跟着变长,管理这个列表将变得困难.Ninject模块是一个好的将我们的类型绑定分离到不同的绑定组的方式,它很容易地将分组组织到不同的文件中.将一个类变成一个Ni ...

  4. cx_Oracle 报错 Reason: image not found

    (Study_env) ➜ DAL python -c "import cx_Oracle"Traceback (most recent call last): File &quo ...

  5. thinkPHP--SQL查询

    一.查询方式 ThinkPHP 提供了三种基本的查询方式:字符串条件查询.索引数组条件查询和对象条件查询. 在大多数情况下,推荐使用索引数组和对象方式作为查询条件,因为会更加安全 1.使用字符串作为条 ...

  6. SQLServer : EXEC和sp_executesql的区别

    MSSQL为我们提供了两种动态执行SQL语句的命令,分别是EXEC和sp_executesql.通常,sp_executesql则更具有优势,它提供了输入输出接口,而EXEC没有.还有一个最大的好处就 ...

  7. iOS强制屏幕旋转

    /** 强制旋转屏幕为纵向 (注:这种方式 键盘不能旋转过来; iOS8.x下 UIAlterView旋转不过来  ) @return */ + (void)rotateOrientationPort ...

  8. Maven本地仓库及远程仓库

    转载自:http://blog.csdn.net/wanghantong/article/details/36427433 Maven 仓库的分类: maven的仓库只有两大类:1.本地仓库 2.远程 ...

  9. android 开发 程序中下载安装APK文件 问题汇总 解析程序包时出现问题

    1 若把APK文件保存到应用程序的files目录下,则一定注意保存时使用 FileOutputStream os = openFileOutput(fileName, MODE_WORLD_READA ...

  10. Vim命令合集

    1.模式切换 三种模式:命令模式,输入模式,底行模式 命令模式与输入模式之间的切换:i esc 命令模式与底行模式的切换:shift + :  esc 2. 插入 i:在当前字符的左边插入 I:在当前 ...