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表达式 ...
随机推荐
- unity3d中检测一个物体是否在摄像机视野范围内
这个脚本最好是把模型对象的锚点设置在最低点.好了直接上脚本.可以直接复制代码,把CS文件拖到一个Camera上,然后把目标拖到targetTran中去就行了. using UnityEngine; u ...
- spring 事务问题
今天碰到一个奇怪的问题,在service中执行方法,调用了两次dao,前面是save,在save后面抛错,竟然没回滚,难道不是一个事务? 后来网上查资料,发现spring的事务回滚必须是运行时异常Ru ...
- LCC
LCC: super vector:
- 使用反射,查找WCF异常类型
//使用System.Reflection,查找System.ServiceModel的异常类型 public void ConsoleException() { ...
- js判断页面是pc打开还是手机打开
<script type="text/javascript"> function browserRedirect() { var sUserAgent = naviga ...
- xml ---DOM操作
package day03.xml; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; ...
- 43、哈工大NLP自然语言处理,LTP4j的测试+还是测试
1.首先需要构建自然语言处理的LTP的框架 (1)需要下载LTP的源码包即c++程序(https://github.com/HIT-SCIR/ltp)下载完解压缩之后的文件为ltp-master (2 ...
- diamond专题(一)– 简介和快速使用
(转自 http://blog.csdn.net/zh_winer/article/details/50395024) 一.概况 diamond是淘宝内部使用的一个管理持久配置的系统,它的特点是简单 ...
- leetCode 354. Russian Doll Envelopes
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...
- --查询nvarchar(max)的表和字段
--查询nvarchar(max)的表和字段 select 'insert into #tempTabelInfo select '''+d.name+''', '''+a.name+''', max ...