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. MongoDB学习笔记(数据操作)

    1.  批量插入:     以数组的方式一次插入多个文档可以在单次TCP请求中完成,避免了多次请求中的额外开销.就数据传输量而言,批量插入的数据中仅包含一份消息头,而多次单条插入则会在每次插入数据时封 ...

  2. Js根据Ip地址自动判断是哪个城市

    var province = '' ;var city = '' ;jQuery.getScript("http://int.dpool.sina.com.cn/iplookup/iploo ...

  3. dell omsa 监控,Nrpe信号量泄露

    ipcs -s | awk '/nrpe/ {print "ipcrm -s ",$2} ' | sh /etc/init.d/dataeng stop /etc/init.d/d ...

  4. vs默认VS Development Sever和用IIS Web Server的一点差别

    关于VS Development Server(vs调试默认运行环境)和IIS Web Server 做运行服务器时,请求处理的一点区别. 将请求粗略分为两类:静态资源请求和动态资源请求. 静态资源请 ...

  5. Lambda GroupBy Sum

    DataTable dt = new DataTable(); dt.AsEnumerable().GroupBy(r => r["ShopName"]) .Select(g ...

  6. 用R实现全排列的分类

    R 其实是个很好用的东东哦-最近写了个小函数,可以实现全排列数的枚举,代码如下: permut<-function(seq){     seq_len=length(seq);     if(s ...

  7. IOS系列swift语言之课时三

    今天需要掌握的内容就是:闭包.类.结构体.属性(计算属性和延迟属性) 同样里面有一些题目,有兴趣的可以做一下. 首先我们需要知道什么是闭包?所谓的闭包就是一个代码块(一般是指函数以及被它捕获的成员变量 ...

  8. Math DayTwo

    (1)Excel Sheet Column Number 解题思路:将26进制的数转化为10进制 代码如下: public class Solution { public int titleToNum ...

  9. linux无线配置

    1. 找 wifi 热点 sudo iwlist scan 2. 生成热点的配置(wpa2加密) sudo wpa_passphrase "wifi热点名称" "wpa密 ...

  10. JSP开发模式2_JSP/Servlet/JavaBean(简单注册功能)

    import java.util.regex.Matcher;import java.util.regex.Pattern; public class RegisterBean {    privat ...