[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的外部路径,该元素就没有意义.他由文档本身之外的一个图像文件来替换 ...
随机推荐
- phalcon count统计
单表count: //How many robots are there? $number = Robots::count(); echo "There are ", $numbe ...
- noip赛前小结4
真正的勇士,敢于面对惨淡的人生. 真正的OIer,敢于做ccop的题. 有种凄凉叫做这道数学题已经超出了我语文的理解范围. 有种愤怒叫做ccop类的信息题已经超出了我语文的理解范围和数学的理解范围. ...
- 如何让div中的内容垂直居中
虽然Div布局已经基本上取代了表格布局,但表格布局和Div布局仍然各有千秋,互有长处.比如表格布局中的垂直居中就是Div布局的一大弱项,不过好在千变万化的CSS可以灵活运用,可以制作出准垂直居中效果, ...
- js字符串函数之split()join()
split方法用于把一个字符串切割成字符串数组,与join相反 一个参数表示以该参数为切割点, var str="silence's world"; console.log(str ...
- 【MYSQL】update/delete/select语句中的子查询
update或delete语句里含有子查询时,子查询里的表不能在update或是delete语句中,如含有运行时会报错:但select语句里含有子查询时,子查询里的表可以在select语句中. 如:把 ...
- jdbc 配置properties实现
package com.web.study; import java.io.InputStream; import java.sql.Connection; import java.sql.Drive ...
- Java面试题(1)- 高级特性
1. The diffrence between java.lang.StringBuffer and java.lang.StringBuilder? java.lang.StringBuffer: ...
- linux service等命令不能使用的解决办法
主要是路径没添加进来: 在shell里面输入这条命令:export PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin ...
- 使用FIDDER 抓包构建请求
FIDDER 是一个抓包利器,可以抓去浏览器的http请求. 工作原理是: FIDDER 作为代理. 1.在启动fidder时他自动启动服务监听8888端口. 2.启动FIDDER它会自动修改 ...
- Ubuntu里面的安装命令总结
本人是新手中的新手,才开始用ubuntu.下面,总结一下安装软件的方法...... 0. 利用apt-get 其实,在ubuntu下安装软件的方法其实灰常简单.就是在终端里面输入: sudo apt- ...