[C/C++]C++声明
【注】本文是Declarations的翻译和注解版。
https://msdn.microsoft.com/en-us/library/f432x8c6.aspx
1、声明:
我们通过声明往C++程序中引入(或重新引入)名字。Definitions are declarations that are sufficient to use the entity identified by the name(不知如何翻译,意思是定义也是一种声明)。不同种类的实体其声明也是不同的,具体如下所示:
- 函数声明(Function declaration)
- 模板声明(Template declaration)
- 显式模板实例化(Explicit template instantiation)
- 显式模板具体化(Explicit template specialization)
- 命名空间定义(Namespace definition)
- 链接具体化(Linkage specification)
- 属性声明(Attribute declaration)(C++11)
- 空声明(Empty declaration)
- 块声明(Block declaration)(【注】块声明指可以出现在块中的声明)。它们可以是以下的声明:
-
- asm definition
- type alias declaration
- namespace alias definition
- using declaration
- using directive
- static assert declaration
- opaque enum declaration
- 简单声明(simple declaration)
简单声明是一条引入、创建一个或多个标识符(一般是变量)的语句。其语法规则如下:
attr(optional) decl-specifier-seq(optional) init-declarator-list(optional) ;
(1)
- attr(C++11) 任意数目的属性序列。
- decl-specifier-seq 类型修饰符序列。它仅在声明构造函数,析构函数和用户定义类型转换函数(type conversion functions)时是可选的。
- init-declarator-list 逗号分隔的声明符(每个声明符可以有初始化值)列表。当声明有名类/结构体/联合体或有名枚举变量时Init-declarator-list是可选的。
1.1、修饰符(Specifiers)
decl-specifier-seq是由一个或多个修饰符以空白符间隔组成。修饰符有以下六大类:
- typedef修饰符:如果出现这个修饰符,整个声明就是一个类型定义声明,每个声明符都引入了一个新的类型名,而不是一个对象或函数。
- 函数修饰符(inline、virtual、explicit):这三个修饰符只能在函数声明中使用。
- friend修饰符:这个修饰符只能在函数或类类型声明中使用。
- constexpr修饰符:这个修饰符可以在变量定义,函数、函数模板声明,字面量类型静态数据成员的声明中使用。
- 存储类型修饰符(register、static、thread local(C++11)、extern、mutable):一个声明中只能使用一种存储类型修饰符,但是thread local可以与extern或static一起出现。
- 类型修饰符,又可以细分为以下几种情况:
-
- class修饰符(class specifier)
- enum修饰符(enum specifier)
- 简单类型修饰符(simple type specifier)
-
- 基本类型(fundamental type、base type)关键字:char, char16_t, char32_t (C++11), wchar_t, bool, short, int, long, signed, unsigned, float, double, void
- auto(C++11)
- decltype修饰符(C++11)
- 先前声明过的类类型名(限定或未限定)
- 先前声明过的枚举类型名(限定或未限定)
- 先前声明过的typedef名或type alias(C++11)(限定或未限定)
- any keyword that names a fundamental type: char, char16_t, char32_t (since C++11), wchar_t, bool, short, int, long, signed, unsigned, float, double, void
(since C++11) |
-
-
- previously declared class name (optionally qualified)
- previously declared enum name (optionally qualified)
- previously declared typedef-name or type alias (since C++11) (optionally qualified)
- template name with template arguments (optionally qualified, optionally using template disambiguator)
- elaborated type specifier
-
- the keyword class, struct, or union, followed by the identifier (optionally qualified), previously defined as the name of a class, struct, or union.
- the keyword class, struct, or union, followed by template name with template arguments (optionally qualified, optionally using template disambiguator), previously defined as the name of a class template.
- the keyword enum followed by the identifier (optionally qualified), previously declared as the name of an enumeration.
-
- only one type specifier is allowed in a decl-specifier-seq, with the following exceptions:
- -
const
can be combined with any type specifier except itself. - -
volatile
can be combined with any type specifier except itself. - -
signed
orunsigned
can be combined withchar
,long
,short
, orint
. - -
short
orlong
can be combined withint
. - -
long
can be combined withdouble
.
|
(since C++11) |
Attributes may appear in decl-specifier-seq, in which case they apply to the type determined by the preceding specifiers.
The only specifier that is allowed to appear twice in a decl-specifier-seq is |
(since C++17) |
[C/C++]C++声明的更多相关文章
- Fis3的前端工程化之路[三大特性篇之声明依赖]
Fis3版本:v3.4.22 Fis3的三大特性 资源定位:获取任何开发中所使用资源的线上路径 内容嵌入:把一个文件的内容(文本)或者base64编码(图片)嵌入到另一个文件中 依赖声明:在一个文本文 ...
- Js 变量声明提升和函数声明提升
Js代码分为两个阶段:编译阶段和执行阶段 Js代码的编译阶段会找到所有的声明,并用合适的作用域将它们关联起来,这是词法作用域的核心内容 包括变量声明(var a)和函数声明(function a(){ ...
- 【WCF】错误协定声明
在上一篇烂文中,老周给大伙伴们介绍了 IErrorHandler 接口的使用,今天,老周补充一个错误处理的知识点——错误协定. 错误协定与IErrorHandler接口不同,大伙伴们应该记得,上回我们 ...
- HTML文档声明
前面的话 HTML文档通常以类型声明开始,该声明将帮助浏览器确定其尝试解析和显示的HTML文档类型.本文将详细介绍文档声明DOCTYPE 特点 文档声明必须是HTML文档的第一行.且顶格显示, ...
- Hibernate中事务声明
Hibernate中JDBC事务声明,在Hibernate配置文件中加入如下代码,不做声明Hibernate默认就是JDBC事务. 一个JDBC 不能跨越多个数据库. Hibernate中JTA事务声 ...
- MFC中成员变量的声明顺序与析构顺序
第一次用博客,第一篇随笔,就写今天遇到的一个问题吧. 在VS2008的MFC对话框程序,窗口成员变量的声明顺序与其析构顺序相反,即,先声明的变量后析构,后声明的变量先析构.未在其他模式下测试. cla ...
- C++模板编程:如何使非通用的模板函数实现声明和定义分离
我们在编写C++类库时,为了隐藏实现,往往只能忍痛舍弃模版的强大特性.但如果我们只需要有限的几个类型的模版实现,并且不允许用户传入其他类型时,我们就可以将实例化的代码放在cpp文件中实现了.然而,当我 ...
- 错误: 从内部类中访问本 地变量vvv; 需要被声明为最终类型
从github 下载了源码, 进行编译, 出现了下面的错误 E:\downloads\ff\elasticsearch-master\elasticsearch-master>GRADLE :b ...
- js变量声明作用域问题
1.先来看两个题 var a = 1; foo1(); function foo1(){ console.log(a); //输出1 }; foo2(); var a = 1; function fo ...
- CSS权威指南之css声明,伪类,文本处理--(简要笔记一)
1.css层叠的含义 后面的会覆盖前面的样式 2.每个元素生成一个框,也称盒. 3.替换元素和非替换元素. img如果不指定src的外部路径,该元素就没有意义.他由文档本身之外的一个图像文件来替换 ...
随机推荐
- Python开发者须知 —— Bottle框架常见的几个坑
Bottle是一个小巧实用的python框架,整个框架只有一个几十K的文件,但却包含了路径映射.模板.简单的数据库访问等web框架组件,而且语法简单,部署方便,很受python开发者的青睐.Pytho ...
- 深入理解JVM虚拟机-7虚拟机类加载机制
虚拟机把描述类的数据从Class文件夹加载到内存,并对数据进行小燕.转换解析和初始化,最终形成可以被虚拟机直接使用的java类型,这就是虚拟机的类加载机制. 下面所说的Class文件不是具体的某个文件 ...
- jsp页面 直接从地址栏中 获取 参数的方法
function GetQueryString(name) { var reg = new RegExp("(^|&)"+ name +"=([^&am ...
- SpringMVC--xxx.xml配置
<!--configure the setting of springmvcDispatcherServlet and configure the mapping--> <servl ...
- WLAN频段介绍-04
ISM频段 ISM频段,此频段主要是开放给工业.科学.医学三个主要机构使用,该频段是依据美国联邦通讯委员会(FCC)所定义出来,并没有所谓使用授权的限制. 工业频段:美国频段为902-928MHz,欧 ...
- python获取文件时间
import time, os create_time = time.ctime(os.path.getctime(filename)) modify_time = time.ctime(os.pat ...
- 用Visual C#向access添加数据
(1)创建并打开一个OleDbConnection对象. (2)创建一个插入一条记录的SQL语句. (3)创建一个OleDbCommand对象. (4)通过此OleDbCommand对象完成对插入一条 ...
- groovy基础
字符串字面值 def age=25 log.info 'My age is ${age}' log.info "my age is \${age}" log.info " ...
- mustache模板技术
一.简介Web 模板引擎是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,通常是标准的 HTML 文档.当然不同的开发语言有不同模板引擎,如 Javascript 下的 Hog ...
- RRDTool 存储原理简介——基于时间序列的环型数据库
转自:http://www.jianshu.com/p/b925b1584ab2 RRDTool是一套监测工具,可用于存储和展示被监测对象随时间的变化情况.比如,我们在 Windows 电脑上常见的内 ...