2-3. Using Type Deduction
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的更多相关文章
- 现代C++之理解模板类型推断(template type deduction)
理解模板类型推断(template type deduction) 我们往往不能理解一个复杂的系统是如何运作的,但是却知道这个系统能够做什么.C++的模板类型推断便是如此,把参数传递到模板函数往往能让 ...
- [Effective Modern C++] Item 2. Understand auto type deduction - 了解auto类型推断
条款二 了解auto类型推断 基础知识 除了一处例外,auto的类型推断与template一样.存在一个直接的从template类型推断到auto类型推断的映射 三类情况下的推断如下所示: // ca ...
- [Effective Modern C++] Item 1. Understand template type deduction - 了解模板类型推断
条款一 了解模板类型推断 基本情况 首先定义函数模板和函数调用的形式如下,在编译期间,编译器推断T和ParamType的类型,两者基本不相同,因为ParamType常常包含const.引用等修饰符 t ...
- C++ Templates (1.2 模板实参推断 Template Argument Deduction)
返回完整目录 目录 1.2 模板实参推断 Template Argument Deduction 1.2 模板实参推断 Template Argument Deduction 当调用函数模板(如max ...
- 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 ...
- 2-4. Using auto with Functions
在C++14中允许使用type deduction用于函数参数和函数返回值 Return Type Deduction in C++11 #include <iostream> using ...
- Modern C++ CHAPTER 2(读书笔记)
CHAPTER 2 Recipe 2-1. Initializing Variables Recipe 2-2. Initializing Objects with Initializer Lists ...
- c++ 相关的技术资源整理归类
最近一段时间 c++ 社区里最火热的话题莫过于 cppcon2015 了, isocpp 上一堆相关的新闻,其中有一个页面罗列了该会议的全部主题, 匆匆一瞥几乎眼花缭乱,为期一个星期的会议竟有上百个演 ...
- c++11新特性(了解)
从C++出来到现在已经13年了. Bjarne Stroustrup(C++的创造者)最近评价C++:”感觉像个新的语言“. 事实上,C++11核心已经发生了很重大的变化: . 支持Lambda表达式 ...
随机推荐
- java 静态代码块 构造块 构造方法
class className{ static{ }//静态代码块 { }//构造代码块 public className(){} //构造方法 }
- <fmt:formatDate>标签的输出格式:
<fmt:formatDate>标签的输出格式: d 月中的某一天.一位数的日期没有前导零. dd 月中的某一天.一位数的日期有一个前导零. ddd 周中 ...
- Maven 笔记
maven DOS 打包命令:maven项目 cd 进入项目根目录执行 mav clean package;
- XidianOJ 1195 Industry of Orz Pandas
--正文 贪心 排序好慢慢找就好 #include <iostream> #include <cstring> #include <cstdio> #include ...
- MVC过滤器详解
MVC过滤器详解 APS.NET MVC中(以下简称"MVC")的每一个请求,都会分配给相应的控制器和对应的行为方法去处理,而在这些处理的前前后后如果想再加一些额外的逻辑处理. ...
- 滴滴与Uber的竞争分析
滴滴与Uber的竞争分析 随着互联网时代的到来,智能手机的普及,互联网不再是一个完全虚拟的东西,它开始慢慢地融入到我们的生活中来.这些年我们可以明显地感受到我们的生活方式在一天天发生着变化,我们也逐渐 ...
- Java Daemon 守护线程
Java中可以通过Thread或ThreadGroup的setDaemon方法将线程设置为守护线程 当所有非守护线程退出后 守护线程将被杀死不在运行 .Net中可以通过设置IsBackground属性 ...
- BulkSqlCopy 批量导入数据(Ef支持)
Ado.net对批量数据的支持相信大家都已经非常熟悉.再此就不在多说,就当是给自己备个份,没办法,这个方法太好用了. public static void BulkCreate( string tab ...
- Play1+angularjs+bootstrap ++ (idea + livereload)
我的web开发最强组合:Play1+angularjs+bootstrap ++ (idea + livereload) 时间 2012-12-26 20:57:26 Freewind.me原文 ...
- SQL Lazy Spool Eager Spool
https://www.simple-talk.com/sql/learn-sql-server/showplan-operator-of-the-week-lazy-spool/ The Lazy ...