C ++预处理器#define一个关键字。它是否符合标准? [英] C++ preprocessor #define-ing a keyword. Is it standards conforming?
问题描述
帮助解决评论中发生的争论,关于bool和1的问题:
Help settle the debate that's going on in the comments at this question about bool and 1:
一个符合标准的C ++预处理器允许使用 #define
重新定义语言关键字?
Can a standards-conforming C++ preprocessor allow one to use #define
to redefine a language keyword? If so, must a standards-conforming C++ preprocessor allow this?
如果C ++程序重新定义了一个语言关键字,该程序本身是否符合标准?
If a C++ program redefines a language keyword, can that program itself be standards conforming?
推荐答案
在C ++中,禁止 #define
关键字的最接近的是§17.4。 3.1.1 / 2,其仅在包括标准库头部的翻译单元中不允许:
In C++, the closest thing to forbidding #define
ing a keyword is §17.4.3.1.1/2, which only disallows it in a translation unit that includes a standard library header:
包括头部的翻译单元不应包含定义在该头中声明或定义的名称的任何宏。
A translation unit that includes a header shall not contain any macros that define names declared or defined in that header. Nor shall such a translation unit define macros for names lexically identical to keywords.
该段落的第二句话已经改变了在C ++ 0x到outright forbid #define
关键字(C ++ 0x FCD§17.6.3.3.1):
The second sentence of that paragraph has been changed in C++0x to outright forbid #define
ing a keyword (C++0x FCD §17.6.3.3.1):
翻译单元不得#define或#undef与关键字在字面上的意思相同。
编辑:正如Ken Bloom在对他的回答的评论,规则在C ++ 0x没有改变;文本刚刚重新排列,混淆像我这样的人。 : - )
As pointed out by Ken Bloom in comments to his answer, the rules have not changed in C++0x; the text has just been rearranged to confuse people like me. :-)
这篇关于C ++预处理器#define一个关键字。它是否符合标准?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!