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. My SQL外键约束

    外键约束对子表的含义:如果在父表中找不到对应的候选键,则不能对子表进行insert/update操作 外键约束对父表的含义:在父表上进行update/delete以更新或删除在子表中有一条或多条对应匹 ...

  2. Table of Contents ---BCM

    Table of ContentsAbout This Document................................................................ ...

  3. NSSortDescriptor对象进行数组排序

    //创建一个数组 NSArray *array = @[@"zhangsan", @"lisi", @"zhonger", @"z ...

  4. Ubuntu 12.04搭建l2tp服务器记录。

    1. 安装openswan apt-get install openswan 2.打开 /etc/ipsec.conf 文件,做如下配置: 其中,virtual_privat这里包含的网络地址允许配置 ...

  5. XidianOJ 1020 ACMer去刷题吧

    题目描述 刷题是每个ACMer必由之路,已知某oj上有n个题目,第i个题目小X能做对的概率为Pi(0<=Pi<=1,1<=i<=n) 求小X至少做对k道题的概率 输入 第一行输 ...

  6. linux 学习 13 系统管理

    第十三讲 Linux系统管理 13.1.1进程管理-进程查看 13.1.2进程管理-终止进程 .2工作管理 .3系统资源查看 .4系统定时任务 13.1.1进程管理-进程查看 .进程简介 进程是正在 ...

  7. Redis从基础命令到实战之列表类型(List)

    经过上一篇基于Redis散列类型的改造后,实战练习中的商品管理已经具备了增加.修改整体.修改部分属性和分页查询功能,但仍然不支持删除商品的功能.这是因为商品总数是以一个自增数字记录的,且关联了新商品k ...

  8. c++ 成员函数

    #include <iostream> #include "Sales_item.h" int main() { Sales_item item1, item2; st ...

  9. 顶点着色器详解 (Vertex Shaders)

    学习了顶点处理,你就知道固定功能流水线怎么将顶点从模型空间坐标系统转化到屏幕空间坐标系统.虽然固定功能流水线也可以通过设置渲染状态和参数来改变最终输出的结果,但是它的整体功能还是受限.当我们想实现一个 ...

  10. centos 6.5 查看、开启,关闭 端口

    查看所有端口   netstat -ntlp   1.开启端口(以80端口为例)         方法一:            /sbin/iptables -I INPUT -p tcp --dp ...