Type Deduction 发生在编译时期

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

下面有两个例子:

1. Using auto with a class

 #include <iostream>
#include <typeinfo> using namespace std; class MyClass
{
}; int main()
{
auto variable = MyClass();
cout << "Type of variable: " << typeid(variable).name() << endl;
return ;
}

2.Using auto with Uniform Initialization

#include <iostream>
#include <typeinfo>
using namespace std; class MyClass
{
}; int main()
{
auto variable{ };
cout << "Type of variable: " << typeid(variable).name() << endl;
auto variable2{ MyClass{} };
cout << "Type of variable: " << typeid(variable2).name() << endl;
return ;
}

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. solr5.5教程-Analyzer、Tokenizer、Filter

    对于文本,solr在建立索引和搜索的时候需要对其做一定的处理(比如英文要去掉介词.转成小写.单词原形化等,中文要恰当地要分词).这些工作,一般由Analyzers.Tokenizers.和Filter ...

  2. git和nginx安装

    原始地址: https://www.zybuluo.com/freeethy/note/192109 git安装 设置git的username和email (注册gitlab的账号密码) $$ git ...

  3. mysql中修改view的definer

    我常用的工具是navicat,但是修改definer不能用工具,只能用命令行: 数据库迁移到其他服务器上,会报definer错误,修改view的definer方法如下(比如把definer改为本地的, ...

  4. ActiveMQ实现负载均衡+高可用部署方案

    一.架构和技术介绍 1.简介 ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.完全支持JMS1.1和J2EE 1.4规范的JMS Provider实现 2.activemq的特 ...

  5. js类式继承模式学习心得

    最近在学习<JavaScript模式>,感觉里面的5种继承模式写的很好,值得和大家分享. 类式继承模式#1--原型继承 方法 让子函数的原型来继承父函数实例出来的对象 <script ...

  6. vim中替换命令

    在VIM中进行文本替换:       1.  替换当前行中的内容:    :s/from/to/    (s即substitude)         :s/from/to/     :  将当前行中的 ...

  7. ---bind 配置

    http://blog.csdn.net/zjunjun/article/details/7419125

  8. IIS7.5 webapi 不支持 Delete、Put 解决方法

    在IIS管理界面选择API的项目,选择 “Features View”. 2.  选择 “Handler Mappings” 菜单. 3. 打开“WebDAV” 选项. 4. 点击 “Request ...

  9. Web前端开发:为何选择MVVM而非MVC

    在Web中充斥着所谓的MVC框架,而在我看来,因为一些关键性的技术原因,MVC在Web前端开发中根本无法使用(对的,是无法,而不是不该) 在Web中充斥着所谓的MVC框架,而在我看来,因为一些关键性的 ...

  10. 一步一步学WebSocket (一) 初识WebSocket

    众所周知,Http协议是无状态的,并且是基于Request/Response的方式与服务器进行交互,也就是我们常说的单工模式.但是随着互联网的发展,浏览器与服务端进行双向通信需求的增加,长轮询向服务器 ...