数据类型 (Data type)

int, long int, double, struct, char *, float [], int (*f)()…

计算机程序构造块

计算机程序构造块是不同大小粒度的计算机程序组成部分,它包括变量、表达式、函数或者模块等。

类型系统 (Type System)

类型系统:在编程语言中,“类型系统”是将“type”属性指定给不同计算机程序构造块的规则集
为什么使用类型系统:减少程序中可能出现的bug
1、定义不同程序块之间的接口
2、检查多个块之间是否以一致的方式连接在一起
类型系统分为:静态类型和动态类型
类型系统存在于我们编写程序中,编译器会帮我们检查类型系统。
如果检查发生在编译期(编译的时候检查类型系统是否一致),称为静态类型
如果检查发生在运行期(执行的时候检查类型系统是否一致),称为动态类型
如果检查同时存在与编译期间和运行期间,称为混合类型
C、C++、Java是静态类型
Python是动态类型

Automatic Type Deduction: auto (C++11) 自动类型推导:auto关键字

C++03及之前的标准种,auto放在变量声明之前,声明变量的存储策略。但是这个关键字常省略不写。
C++11中,auto关键字放在变量之前,作用是在声明变量的时候根据变量初始值的类型自动为此变量选择匹配的类型
例:

int a = 10;
auto au_a = a;//自动类型推断,au_a为int类型
cout << typeid(au_a).name() << endl;

result:

int

注意点:
1、auto 变量必须在定义时初始化,这类似于const关键字

auto a1 = 10;  //正确
auto b1;   //错误,编译器无法推导b1的类型
b1 = 10;

2、定义在一个auto序列的变量必须始终推导成同一类型

auto a4 = 10, a5{20};   //正确
auto b4{10}, b5 = 20.0; //错误,没有推导为同一类型

3、如果初始化表达式是引用或const,则去除引用或const语义。

int a{10}; int &b = a;
auto c = b;   //c的类型为int而非int&(去除引用)
const int a1{10};
auto b1 = a1; //b1的类型为int而非const int(去除const)

4、如果auto关键字带上&号,则不去除引用或const语意

int a = 10; int& b = a;
auto& d = b;//此时d的类型才为int&
const int a2 = 10;
auto& b2 = a2;//因为auto带上&,故不去除const,b2类型为const in

5、初始化表达式为数组时,auto关键字推导类型为指针

int a3[3] = { 1, 2, 3 };
auto b3 = a3;
cout << typeid(b3).name() << endl; //输出int * (输出与编译器有关)

6、若表达式为数组且auto带上&,则推导类型为数组类型

int a7[3] = { 1, 2, 3 };
auto& b7 = a7;
cout << typeid(b7).name() << endl; //输出int [3] (输出与编译器有关)

7、C++14中,auto可以作为函数的返回值类型和参数类型

auto max(int x , int y)
{return x>y?x:y;
}

我们在使用auto时有时会遇到一些特殊情况。

1、 “int x = 3;” 能变成auto形式吗?
当我们非常希望能够在变量定义的时候,【明确】地指出变量的类型,而且不希望随便更改其类型,那么我们可以使用下面的方法:

auto x = int {3};  // 初始化列表auto y = int {3.0}; // 编译器报错,初始化列表不能窄化auto z = int (3.0);  // C风格的强制类型转换,z的值是整数3

2、 auto和初始化列表一起用 要避免在一行中使用直接列表初始化和拷贝列表初始化,也就是,下面的代码是有问题的:

auto x { 1 }, y = { 2 };  // 不要同时使用直接和拷贝列表初始化


“AAA原则”:总是使用auto!
问题:能用auto声明C风格的数组吗?
如果你尝试让C++11编译器编译如下代码,它会报错

// 包含头文件
// 声明主函数
// ......
auto x[] = {1,2,3};

auto 不能用于声明数组,否则无法通过编译,报auto类型不能出现在顶级数组类型中。
也就是说auto只能是指针指向数组的而不能去声明一个数组。

自动类型推导:decltype关键字

1、关键字decltype的用法
decltype利用已知类型声明新变量。
有了auto,为什么还要整出一个decltype?原因是,我们有时候想要从表达式的类型推断出要定义的变量类型,但不想用该表达式的值初始化变量。
decltype是在编译期推导一个表达式的类型,而不用初始化,其语法格式有点像sizeof。它只做静态分析,因此它不会导致已知类型表达式执行。
decltype 主要用于泛型编程(模板)
2、代码示例

#include<iostream>
using namespace std;
int  fun1()  { return 10;  }
auto fun2()  { return 'g'; }  // C++14
int main(){// Data type of x is same as return type of fun1()// and type of y is same as return type of fun2()decltype(fun1()) x;  // 不会执行fun1()函数,相当于 int x;decltype(fun2()) y = fun2();       //auto y=>char y;cout << typeid(x).name() << endl;cout << typeid(y).name() << endl;return 0;
}

我们可以发现,我们将fun2的‘g’改变,例如改成4,y类型也改了。
这样我们修改了返回值类型,程序仍然能够正常运行。
这说明,使用类型推导是有好处的。
3、decltype与auto的对比
decltype和auto都是C++11自动类型推导的关键字。它们有很多差别:
auto忽略最上层的const,decltype则保留最上层的const
auto忽略原有类型的引用,decltype则保留原有类型的引用
对解引用操作,auto推断出原有类型,decltype推断出引用

4、auto推断时会实际执行,decltype不会执行,只做分析。
总之在使用中过程中和const、引用和指针结合时需要特别小心。

小结

C++11对C++03的增强,或者说C++11的“Modern”的特点之一就是由auto和decltype构成的自动类型推导系统。

但是,auto与初始化列表结合,又有一些坑。你在写代码时如果经常将auto与列表初始化一起使用,那么会遇到一些问题。本节只介绍auto的常见用法。auto与初始化列表结合的坑,得由你自己去踩了。

为了说明auto有多么复杂,这里摘取 https://cppreference.com 网站列出的auto的语法格式:

【C++ grammar】Enhancement for Type System (C++11 对类型系统的增强)相关推荐

  1. JsonException: Max allowed object depth reached while trying to export from type System.Single

    在进行类转json字符串时,报错JsonException: Max allowed object depth reached while trying to export from type Sys ...

  2. ABAP的数据字典DDIC和Hybris Commerce的类型系统Type system

    Netweaver里所有global可用的data type定义在DDIC里.Hybris也有类似ABAP DDIC的概念,称为type system, 类型定义存在于海量的xml里.区别在于DDIC ...

  3. The type 'System.Object' is defined in an assembly that is not referenced

    记录一个错误,报 The type 'System.Object' is defined in an assembly that is not referenced,[System.Runtime] ...

  4. Unity3d:Unknown type 'System.Collections.Generic.CollectionDebuggerView'1

    问题描述:如图,在调试状态下说:Unknown type 'System.Collections.Generic.CollectionDebuggerView'1 <ignore_js_op&g ...

  5. C# Predefined type 'System.Object' is not defined or imported

    在写C#的时候,有时候会出现 Severity Code Description Project File Line Suppression State Error CS0518 Predefined ...

  6. 【iis】解决:Could not load type ‘System.ServiceModel.Activation.HttpModule‘ from assemb

    解决:Could not load type 'System.ServiceModel.Activation.HttpModule' from assemb 发布 .svc 项目,获取运行出错如下: ...

  7. Unity打包iOSFailed to resolve base type System.ServiceModel.Configuration.BehaviorExtensionElement for

    使用 Unity 5.4.2f2 打包iOS时,遇到Failed to resolve base type System.ServiceModel.Configuration.BehaviorExte ...

  8. 多态和接口(3)——设计模式(1)——方法override、CLR(Common Language Runtime 公共语言运行时)、CTS(Common Type System 公共语言系统)

    1.多态就是设计模式!!!多态理解了设计模式就理解了. 2.定义坐标类,坐标类默认继承Object的ToString()方法,没有自己的特色. 3.override ToString()输出友好信息: ...

  9. An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module

    今天编写一个c++ clr程序调用c# dll,由于不熟,爆出一个错误 An unhandled exception of type 'System.IO.FileNotFoundException' ...

最新文章

  1. forward 和redirect
  2. mysql知识结构图_MySql知识结构说明
  3. dorado7ajax,Dorado7
  4. employee setup in Organization unit
  5. Java集合(3)--Iterator迭代器
  6. Java并发同步器AQS(AbstractQueuedSynchronizer)学习笔记(2)
  7. 【Elasticsearch】eBay上的Elasticsearch性能调优实践
  8. Oracle数据库的DDL操作
  9. ubuntu 虚拟显示器制作
  10. Android 特效大全代码资源——源码
  11. 中国互联网创业工具库Startup Tools
  12. android星星闪效果,使用Canvas绘制星星闪烁的效果
  13. 图像增强:opencv去除图片的高光
  14. Mac安装brewLast login: Mon Feb 25 22:00:38 on console 192:~ wxh$ /usr/bin/ruby -e $(curl -fsSL https:
  15. 身份证最后一位验证[python]
  16. 中国免疫抑制剂药物市场趋势报告、技术动态创新及市场预测
  17. 数学家对物理学家的鄙视_物理学家的未来经济学
  18. egg(一):创建一个egg项目
  19. TCP/IP篇(1)--协议模型
  20. 是谁发明了光纤?光纤的发明发展历史

热门文章

  1. java中菜单分几级_JavaWeb三级菜单分类查询详解
  2. html5 loaded,How do you check if a HTML5 audio element is loaded?
  3. React使用的扩展
  4. iOS-----------关于组件化
  5. 数独项目--关键代码展示:
  6. MVC View显示详解(RenderBody,RenderPage,RenderSection,Partial)
  7. Common Knowledge_快速幂
  8. ubuntu 开发板ping通虚拟机挂载nfs服务器
  9. Android 之视频监控
  10. 【转】用BibTeX 写 Reference