【注】本文是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)
(since C++11)
  • 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 or unsigned can be combined with char, long, short, or int.
- short or long can be combined with int.
- long can be combined with double.
- long can be combined with long.
(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 long (which can appear twice in a row. All other repeats, such as const static const, or virtual inline virtual are errors.

(since C++17)

[C/C++]C++声明的更多相关文章

  1. Fis3的前端工程化之路[三大特性篇之声明依赖]

    Fis3版本:v3.4.22 Fis3的三大特性 资源定位:获取任何开发中所使用资源的线上路径 内容嵌入:把一个文件的内容(文本)或者base64编码(图片)嵌入到另一个文件中 依赖声明:在一个文本文 ...

  2. Js 变量声明提升和函数声明提升

    Js代码分为两个阶段:编译阶段和执行阶段 Js代码的编译阶段会找到所有的声明,并用合适的作用域将它们关联起来,这是词法作用域的核心内容 包括变量声明(var a)和函数声明(function a(){ ...

  3. 【WCF】错误协定声明

    在上一篇烂文中,老周给大伙伴们介绍了 IErrorHandler 接口的使用,今天,老周补充一个错误处理的知识点——错误协定. 错误协定与IErrorHandler接口不同,大伙伴们应该记得,上回我们 ...

  4. HTML文档声明

    前面的话   HTML文档通常以类型声明开始,该声明将帮助浏览器确定其尝试解析和显示的HTML文档类型.本文将详细介绍文档声明DOCTYPE 特点   文档声明必须是HTML文档的第一行.且顶格显示, ...

  5. Hibernate中事务声明

    Hibernate中JDBC事务声明,在Hibernate配置文件中加入如下代码,不做声明Hibernate默认就是JDBC事务. 一个JDBC 不能跨越多个数据库. Hibernate中JTA事务声 ...

  6. MFC中成员变量的声明顺序与析构顺序

    第一次用博客,第一篇随笔,就写今天遇到的一个问题吧. 在VS2008的MFC对话框程序,窗口成员变量的声明顺序与其析构顺序相反,即,先声明的变量后析构,后声明的变量先析构.未在其他模式下测试. cla ...

  7. C++模板编程:如何使非通用的模板函数实现声明和定义分离

    我们在编写C++类库时,为了隐藏实现,往往只能忍痛舍弃模版的强大特性.但如果我们只需要有限的几个类型的模版实现,并且不允许用户传入其他类型时,我们就可以将实例化的代码放在cpp文件中实现了.然而,当我 ...

  8. 错误: 从内部类中访问本 地变量vvv; 需要被声明为最终类型

    从github 下载了源码, 进行编译, 出现了下面的错误 E:\downloads\ff\elasticsearch-master\elasticsearch-master>GRADLE :b ...

  9. js变量声明作用域问题

    1.先来看两个题 var a = 1; foo1(); function foo1(){ console.log(a); //输出1 }; foo2(); var a = 1; function fo ...

  10. CSS权威指南之css声明,伪类,文本处理--(简要笔记一)

    1.css层叠的含义 后面的会覆盖前面的样式 2.每个元素生成一个框,也称盒.   3.替换元素和非替换元素. img如果不指定src的外部路径,该元素就没有意义.他由文档本身之外的一个图像文件来替换 ...

随机推荐

  1. el 和 fmt 常用

    EL表达式入门 转自 http://blog.chinaunix.net/uid-9789791-id-1997374.html 隐含对象: pageContext: pageContext对象 pa ...

  2. <转> jsp页面向action传值的方法(最后一种简单)

    多的不说,直接上代码; struts.xml代码: <?xml version="1.0" encoding="UTF-8"?> <!DOCT ...

  3. java 多线程8(守护线程)

    比如:后台偷偷运行的那些,qq下载更新包 如果一个进程中只剩下了守护线程,那么守护线程也会死亡.. 一个线程默认都不是守护线程. 判断是否是守护线程:例:d.isDaemon(); 当一个线程随着你的 ...

  4. Git分支管理详解

    内容来源:http://blog.jobbole.com/25775/ Git简介 Git是分布式版本控制工具:Git 收取的是项目历史的所有数据(每一个文件的每一个版本),服务器上有的数据克隆之后本 ...

  5. Shell基础:Linux权限管理

    Linux权限基本概念 查看系统(文件夹/文件)权限: ls -l =>d/-   xxx xxx xxx.  num  owner  group  size   date  filename ...

  6. 【待整理】PS切图基础教程

    http://www.w3cfuns.com/article-442-1-1.html http://www.w3cfuns.com/article-443-1-1.html 其他专题研究: floa ...

  7. IO流--复制picture ,mp3

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import ...

  8. linq分页扩展(转)

    原文地址:http://www.cnblogs.com/RainbowInTheSky/p/4590508.html public static List<T> ToPagedList&l ...

  9. UserAgent:通过浏览器获取用户浏览器等信息

    User Agent的含义      User Agent中文名为用户代理,简称 UA,它是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本.CPU 类型.浏览器及版本.浏览器渲染引擎.浏 ...

  10. Hadoop 添加删除数据节点(datanode)

    前提条件: 添加机器安装jdk等,最好把环境都搞成一样,示例可做相应改动 实现目的: 在hadoop集群中添加一个新增数据节点. 1. 创建目录和用户  mkdir -p /app/hadoop gr ...