[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:
- -
constcan be combined with any type specifier except itself. - -
volatilecan be combined with any type specifier except itself. - -
signedorunsignedcan be combined withchar,long,short, orint. - -
shortorlongcan be combined withint. - -
longcan 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的外部路径,该元素就没有意义.他由文档本身之外的一个图像文件来替换 ...
随机推荐
- AD组策略添加本地账号、设置允许ping回显
AD组策略添加本地账号 1. 管理工具--组策略管理--选择相应GPO(编辑)----首选项--控制面板设置--本地用户和组--右键添加账号 2.域成员计算机刷新组策略(gpupdate/force) ...
- linux笔记:用户管理命令和用户组管理命令
用户管理命令 命令名称:useradd功能:添加用户(添加完后不能立即使用,必须用passwd修改用户密码后才能使用)用法:useradd [选项] 用户名选项参数:-u 手工指定用户的UID-d 手 ...
- 【CITE】C#目录、文件、文件夹操作
1. 在一个目录下创建一个文件夹 if (!System.IO.Directory.Exists(path)) System.IO.Directory.CreateDirectory(path); ...
- python 练习 8
#!/usr/bin/python # -*- coding: utf-8 -*- def ntom(x,size,mod): t=[0]*(size) j=0 while x and j<si ...
- nyoj-----127星际之门(一)
星际之门(一) 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 公元3000年,子虚帝国统领着N个星系,原先它们是靠近光束飞船来进行旅行的,近来,X博士发明了星际之门 ...
- Valid Palindrome [LeetCode]
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- C++编程中const和#define的区别
(1) 编译器处理方式不同 define宏是在预处理阶段展开. const常量是编译运行阶段使用.(2) 类型和安全检查不同 define宏没有类型,不做任何类型检查,仅仅是展开. const常量有具 ...
- springMVC 验证器
采用Hibernate-validator来进行验证,Hibernate-validator实现了JSR-303验证框架支持注解风格的验证.首先我们要到http://hibernate.org/val ...
- Java 集合系列 07 List总结(LinkedList, ArrayList等使用场景和性能分析)
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...
- 20145236 冯佳 《Java程序设计》第1周学习总结
20145236 冯佳 <Java程序设计>第1周学习总结 教材学习内容总结 因为假期在家的时候并没有提前自学Java,所以,这周算是真正开始第一次接触Java.我对Java的了解也仅仅停 ...