c++ - 预处理器 : Meaning of "The definition also permits you to split an identifier at any position and get exactly two tokens"

标签 c++ c-preprocessor token language-lawyer identifier

似乎普遍认为无法将 [GNU CPP] 预处理器 token 拆分为两个 token ,但我在 GNU CPP 手册中找到了这一点,但似乎无法弄清楚,也找不到有关它的更多信息。

这是什么意思? 第 1.3 节标记化:

"A preprocessing number has a rather bizarre definition. The category includes all the normal integer and floating point constants one expects of C, but also a number of other things one might not initially recognize as a number. Formally, preprocessing numbers begin with an optional period, a required decimal digit, and then continue with any sequence of letters, digits, underscores, periods, and exponents. Exponents are the two-character sequences ‘e+’, ‘e-’, ‘E+’, ‘E-’, ‘p+’, ‘p-’, ‘P+’, and ‘P-’. (The exponents that begin with ‘p’ or ‘P’ are new to C99. They are used for hexadecimal floating-point constants.)

The purpose of this unusual definition is to isolate the preprocessor from the full complexity of numeric constants. It does not have to distinguish between lexically valid and invalid floating-point numbers, which is complicated. The definition also permits you to split an identifier at any position and get exactly two tokens, which can then be pasted back together with the ‘##’ operator. "

-- https://gcc.gnu.org/onlinedocs/cpp/Tokenization.html#Tokenization

我有点熟悉使用## 连接两个标记;我理解创造,例如通过本质上使用“var_ ## 1”来实现“var_1”;我不明白的是,为什么“预处理数字”的“奇怪定义”与拆分标识符和粘贴有任何关系。

坦率地说,在我读到该行的前十次时,我肯定地认为它暗示存在一个奇怪的因素,即把一个 token 当作一个数字来对待,这将允许它“在任何位置”被拆分。

最佳答案

The definition also permits you to split an identifier at any position and get exactly two tokens, which can then be pasted back together with the ## operator.

这句话如果更正式地说可能会更清楚:有效标识符的每个前缀和后缀都是有效的预处理标记。此属性有助于预处理器的某些用途。

例如,您可以通过将 a1b2 粘合在一起来构建标识符 a1b21b2 是一个有效的预处理编号(由标准的[lex.ppnumber] 部分定义),尽管它看起来并不多就像一个数字。

关于c++ - 预处理器 : Meaning of "The definition also permits you to split an identifier at any position and get exactly two tokens",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27673449/

相关文章:

c++ - 如何仅在本地 header 上运行预处理器?

c - 带有哈希的预处理器指令

c# - 无法将类型 'System.Security.Cryptography.DSACryptoServiceProvider' 的对象强制转换为类型 'System.Security.Cryptography.RSA'

token - 获取以太坊 ERC-20 代币信息的正确方法

c++ - cin.get() 没有跳出循环

c++ - DirectX9 C++ - 在窗口最小化时加载纹理

java - 如何添加到文件中(在顶部添加)

c++ - 带有 g++ 的不需要的警告消息

c - 如何计算#define 中的逗号

iOS:如何在使用 componentSeparatedByCharactersInSet 时维护分隔符