c++ - 什么是 id 表达式?

标签 c++ language-lawyer

我很难清楚地理解 id-expression 是什么。我将按照我在最新的 C++ 标准工作草案中找到的内容开始:

enter image description here

冒险定义一个标识符:

An identifier is an arbitrarily long sequence of letters and digits.

所以看起来任意长的字母和数字序列都可以是id-expression,但请稍等:

enter image description here

所以 identifier 必须先声明才能成为 id-expression?好吧,让我们转到第 7 条:

enter image description here

继续...

enter image description here

继续……

enter image description here

我们到了:

enter image description here

我将其解释为 id-expression 需要声明一个 identifier,这需要 id-expression。这似乎是一个循环定义。谁能告诉我哪里出错了?

无论如何,我的解释是必须先声明标识符才能将其视为 id-expression,但这不只是一个 name ?该标准规定:

Every name that denotes an entity is introduced by a declaration.

那么为什么不直接将其称为 name-expression 呢?

最佳答案

您误解了 [expr.prim.general]/8 :

An identifier is an id-expression provided it has been suitably declared (Clause 7).

该短语的目的是禁止在表达式中使用未声明的标识符。 IE。本段讨论的是 id-expressions 作为 primary-expressions 出现,而​​不是 id-expressions 出现在声明者。我同意该短语具有误导性,应予以修正。我提交了一个核心问题。

So why not just call it a name-expression instead?

历史原因;标识符和名称之间的细微区别是在非常基本的语法结构(id-expressionunqualified-id 等)被命名之后进行的,然后再重新命名那些不是一个选择。

关于c++ - 什么是 id 表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31440790/

相关文章:

c++ - 如何从数据集中读取特定的字符列

c - void 表达式中的未定义行为

android - AOSP : Unkown C++ headers ( no such file or directory for <vector>, <iostream>, .. >

c# - 将 C++ 数组返回到 C#

c++ - SFINAE 构造问题

c++ - 如何在静态库中的单独 cpp 文件中链接未引用的变量?

c++ - Lambda函数可变捕获从引用到全局变量的行为差异

c++ - N4140 的要点 §5.19/2.3 中的单词 "undefined"是什么意思?

c - C中临时对象的有效类型

c - 外部、内部且没有链接,或者为什么这不起作用?