怎样看待 C++ 中对自定义的标识符的命名规定

C++11 §17.6.4.1 Overview1. This section describes restrictions on C++ programs that use the facilities of the C++ standard library.C++11 §17.6.4.3 Reserved names2. If a program declares or defines a name in a context where it is reserved, other than as explicitly allowed bythis Clause, its behavior is undefined.C++11 §17.6.4.3.2 Global Names1. Certain sets of names and function signatures are always reserved to the implementation:Each name that contains a double underscore _ _ or begins with an underscore followed by an uppercase letter (2.12) is reserved to the implementation for any use.Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace. 歌词大意:如果你要用 C++ 标准库,那么,往下看。(话说如果不用标准库的话用 C++ 有意思么?)任何地方,都保留以下名字(比如可能把它用作宏):以一个下划线、一个大写字母开头的包含连续的两个下划线的在全局命名空间中,保留以下名字:以下划线开头的如果你用了我们的保留的名字,会产生未定义行为。至于「未定义行为」是什么?正确编译?报错?硬盘烧坏?主机爆炸?地震?海啸?革命?都可能。p.s. 其实 C 语言的更复杂:C99 §7.1.3 Reserved identifiersEach header declares or defines all identifiers listed in its associated subclause, andoptionally declares or defines identifiers listed in its associated future library directionssubclause and identifiers which are always reserved either for any use or for use as filescope identifiers.
【怎样看待 C++ 中对自定义的标识符的命名规定】 All identifiers that begin with an underscore and either an uppercase letter or anotherunderscore are always reserved for any use.
All identifiers that begin with an underscore are always reserved for use as identifierswith file scope in both the ordinary and tag name spaces.
Each macro name in any of the following subclauses (including the future librarydirections) is reserved for use as specified if any of its associated headers is included;unless explicitly stated otherwise (see 7.1.4).
All identifiers with external linkage in any of the following subclauses (including thefuture library directions) are always reserved for use as identifiers with externallinkage.160)
Each identifier with file scope listed in any of the following subclauses (including thefuture library directions) is reserved for use as a macro name and as an identifier withfile scope in the same name space if any of its associated headers is included.
No other identifiers are reserved. If the program declares or defines an identifier in acontext in which it is reserved (other than as allowed by 7.1.4), or defines a reservedidentifier as a macro name, the behavior is undefined.
If the program removes (with #undef) any macro definition of an identifier in the firstgroup listed above, the behavior is undefined.

■网友
我今天在C++Prime上也遇到了这个问题,过来挖坟了。
我理解是,标准库一般提供给外部使用的标识符通常采用了下划线开头,但标准并不具体规定每个标识符的名字。或者说,根据标准库的需要,这些标识符是可以增减或者更名的(主要是为了适应新功能进行增加);为了避免升级冲突,应用程序在标识符命名时就应该进行规避~而编译器是不会出现类似报错的。为了避免未知情况,因此一般不使用双下划线组合或者下划线加大写字母组合。


推荐阅读