[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的外部路径,该元素就没有意义.他由文档本身之外的一个图像文件来替换 ...
随机推荐
- Gevent的长轮询实现方法详解
长轮询 1.浏览网页时,浏览器会传HTTP 请求到服务器,服务器会根据请求将网页的内容传给浏览器,但是在很多的情况下,使用者会需要看到最新的即时性资讯,例如观看股票市场行情,而在以前只能靠着重新载入网 ...
- matlab中图像显示函数
image函数是显示图像的最基本的方法.该函数还产生了图像对象的句柄,并允许对对象的属性进行设置. imagesc函数也具有image的功能,所不同的是imagesc函数还自动将输入数据比例化,以全色 ...
- phalcon: 表单
以实例为说明: controller <?php use \Phalcon\Forms\Form; use \Phalcon\Forms\Element\Text; use \Phalcon\F ...
- Machine Learning - 第5周(Neural Networks: Learning)
The Neural Network is one of the most powerful learning algorithms (when a linear classifier doesn't ...
- worker中加载本地文件报错的解决方案
如果在一个swf的主线程中加载文件时,报安全沙箱的错误, 网上有诸多的解决方案.但是如果在一个worker中加载本地文件报类似如下的错误: *** 安全沙箱冲突 *** SecurityError: ...
- hdu----(5055)Bob and math problem(贪心)
Bob and math problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Sudoku Solver [LeetCode]
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 下载模板,上传EXCEL
说道SAP里对EXCEL操作的大概就是上传,下载,显示了... 下载:(文档是通过SMW0上传的)注:如果下载的时候需要填充EXCEL的值,...请参考另一篇文档,OLE CALL METHOD CL ...
- 告别node-forever,拥抱PM2
告别node-forever,拥抱PM2 返回原文英文原文:Goodbye node-forever,hello PM2 devo.ps团队对JavaScript的迷恋已经不是什么秘密了;node.j ...
- eclipse 项目修改和更新项目,回退版本,解决分支的冲突的办法
一个关于git的图 1.我在github建立了3个分支. 2.把其中一个分支拉到本地. 项目修改提交到远程库 3.修改完代码以后commit项目,点击项目右击->team->commit ...