通用(构建)系统上 C/C++ 标识符的最大长度是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6007568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is max length for an C/C++ identifier on common (build) systems?
提问by mbx
I don't remember the standard saying something of the max length for identifiers so in theory they can be long. In real life, those names could be limited by at least the compiler and linker implementations.
我不记得标准说标识符的最大长度,所以理论上它们可以是long。在现实生活中,这些名称至少会受到编译器和链接器实现的限制。
While this should work on all systems
虽然这应该适用于所有系统
int a;
this snippet
这个片段
#!/usr/bin/perl
print "int" . "b" x 2**16 . ";";
creates a declarationen that gives undefined reference to std::
somethings with ld
while compiling/linking (using gcc/mingw).
创建一个declarationen给出未定义参照std::
出头与ld
在编译/链接(使用gcc / MingW平台)。
So what are the size limits for an identifier on different systems?
那么不同系统上标识符的大小限制是多少?
回答by Anders Lindahl
- Microsoft C++: 2048 characters
- Intel C++: 2048 characters
- g++: No limit, as answered in this question: is there a length limit on g++ variable names?
- Microsoft C++:2048 个字符
- 英特尔 C++:2048 个字符
- g++:没有限制,正如在这个问题中所回答的:g++ 变量名是否有长度限制?
回答by mbx
Annex B of the C++ Standard says that an implementation should support identifiers at least 1024 characters long, but this is not mandatory.
C++ 标准的附录 B 说一个实现应该支持长度至少为 1024 个字符的标识符,但这不是强制性的。
回答by SpongeBob
Based on MISRA C 2004:
基于 MISRA C 2004:
Rule 5.1 (required): Identifiers (internal and external) shall not rely on the significance of more than 31 characters. [Undefined 7; Implementation 5, 6] The ISO standard requires internal identifiers to be distinct in the first 31 characters to guarantee code portability. This limitation shall not be exceeded, even if the compiler supports it. This rule shall apply across all name spaces. Macro names are also included and the 31 character limit applies before and after substitution. The ISO standard requires external identifiers to be distinct in the first 6 characters, regardless of case, to guarantee optimal portability. However this limitation is particularly severe and is considered unnecessary. The intent of this rule is to sanction a relaxation of the ISO requirement to a degree commensurate with modern environments and it shall be confirmed that 31 character/ case significance is supported by the implementation. Note that there is a related issue with using identifier names that differ by only one or a few characters, especially if the identifier names are long. The problem is heightened if the differences are in easily mis-read characters like 1 (one) and l (lower case L), 0 and O, 2 and Z, 5 and S, or n and h. It is recommended to ensure that identifier names are always easily visually distinguishable. Specific guidelines on this issue could be placed in the style guidelines (see section 4.2.2).
规则 5.1(必需):标识符(内部和外部)不得依赖于超过 31 个字符的重要性。[未定义 7; 实现 5, 6] ISO 标准要求内部标识符在前 31 个字符中是不同的,以保证代码的可移植性。即使编译器支持,也不得超过此限制。此规则适用于所有名称空间。还包括宏名称,并且在替换前后均适用 31 个字符的限制。ISO 标准要求外部标识符在前 6 个字符中是不同的,无论大小写,以保证最佳的可移植性。然而,这种限制特别严重,被认为是不必要的。该规则的目的是批准将 ISO 要求放宽到与现代环境相称的程度,并应确认实施支持 31 个字符/案例的重要性。请注意,使用仅相差一个或几个字符的标识符名称存在一个相关问题,尤其是在标识符名称很长的情况下。如果差异在于容易误读的字符,如 1(一)和 l(小写 L)、0 和 O、2 和 Z、5 和 S,或 n 和 h,问题就会加剧。建议确保标识符名称始终易于视觉区分。关于这个问题的具体指南可以放在风格指南中(见第 4.2.2 节)。请注意,使用仅相差一个或几个字符的标识符名称存在一个相关问题,尤其是在标识符名称很长的情况下。如果差异在于容易误读的字符,如 1(一)和 l(小写 L)、0 和 O、2 和 Z、5 和 S,或 n 和 h,问题就会加剧。建议确保标识符名称始终易于视觉区分。关于这个问题的具体指南可以放在风格指南中(见第 4.2.2 节)。请注意,使用仅相差一个或几个字符的标识符名称存在一个相关问题,尤其是在标识符名称很长的情况下。如果差异在于容易误读的字符,如 1(一)和 l(小写 L)、0 和 O、2 和 Z、5 和 S,或 n 和 h,问题就会加剧。建议确保标识符名称始终易于视觉区分。关于这个问题的具体指南可以放在风格指南中(见第 4.2.2 节)。建议确保标识符名称始终易于视觉区分。关于这个问题的具体指南可以放在风格指南中(见第 4.2.2 节)。建议确保标识符名称始终易于视觉区分。关于这个问题的具体指南可以放在风格指南中(见第 4.2.2 节)。
I use this rule. Maybe somebody ask me "Do you see any compiler that can't recognize identifier more than 31?" Yes, I remember in IAR RL78 v2.21.1 I define 2 identifier without any warning and error I faced problem in access(now I don't remember what was the exact scenario)
我用这个规则。也许有人会问我“你看到任何不能识别超过 31 标识符的编译器了吗?” 是的,我记得在 IAR RL78 v2.21.1 中我定义了 2 个标识符,没有任何警告和错误我在访问中遇到问题(现在我不记得确切的场景是什么)