The rules (which did not change in C++11):
(规则(在C ++ 11中未更改):)
- Reserved in any scope, including for use as implementation macros:
(在任何范围内都保留,包括用作实现宏:)
- identifiers beginning with an underscore followed immediately by an uppercase letter
(以下划线开头的标识符,紧随其后的是大写字母)
- identifiers containing adjacent underscores (or "double underscore")
(包含相邻下划线(或“双下划线”)的标识符)
- Reserved in the global namespace:
(在全局名称空间中保留:)
- identifiers beginning with an underscore
(下划线开头的标识符)
- Also, everything in the
std
namespace is reserved. (同样, std
名称空间中的所有内容均保留。)
(You are allowed to add template specializations, though.) ((不过,您可以添加模板专长。))
From the 2003 C++ Standard:
(根据2003 C ++标准:)
17.4.3.1.2 Global names [lib.global.names] (17.4.3.1.2全局名称[lib.global.names])
Certain sets of names and function signatures are always reserved to the implementation:
(某些名称和函数签名集始终保留给实现:)
165) Such names are also reserved in namespace ::std
(17.4.3.1).
(165)这些名称也保留在命名空间::std
(17.4.3.1)中。)
Because C++ is based on the C standard (1.1/2, C++03) and C99 is a normative reference (1.2/1, C++03) these also apply, from the 1999 C Standard:
(由于C ++基于C标准(1.1 / 2,C ++ 03),而C99是规范性参考文献(1.2 / 1,C ++ 03),这些也适用于1999 C标准:)
7.1.3 Reserved identifiers (7.1.3保留标识符)
Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers.
(每个标头声明或定义在其关联的子节中列出的所有标识符,并可选地声明或定义在其关联的将来的库指示子节中列出的标识符以及始终保留用于任何用途或用作文件范围标识符的标识符。)
- All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.
(以下划线,大写字母或另一个下划线开头的所有标识符始终保留供任何使用。)
- All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.
(在普通和标记名称空间中,所有以下划线开头的标识符始终保留为具有文件范围的标识符。)
- Each macro name in any of the following subclauses (including the future library directions) is reserved for use as specified if any of its associated headers is included;
(如果包含任何关联的标头,则以下任何一个子节(包括将来的库说明)中的每个宏名均保留为指定用途。)
unless explicitly stated otherwise (see 7.1.4). (除非另有明确说明(请参阅7.1.4)。)
- All identifiers with external linkage in any of the following subclauses (including the future library directions) are always reserved for use as identifiers with external linkage.
(在以下任何条款(包括将来的库说明)中,所有具有外部链接的标识符始终保留为具有外部链接的标识符。)
154 (154)
- Each identifier with file scope listed in any of the following subclauses (including the future library directions) is reserved for use as a macro name and as an identifier with file 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 a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined. (如果程序在保留它的上下文中声明或定义标识符(而不是7.1.4允许),或者将保留标识符定义为宏名,则该行为未定义。)
If the program removes (with #undef
) any macro definition of an identifier in the first group listed above, the behavior is undefined.
(如果程序删除(使用#undef
)上面列出的第一组中标识符的任何宏定义,则该行为未定义。)
154) The list of reserved identifiers with external linkage includes errno
, math_errhandling
, setjmp
, and va_end
.
(154)具有外部链接的保留标识符的列表包括errno
, math_errhandling
, setjmp
和va_end
。)
Other restrictions might apply.
(其他限制可能适用。)
For example, the POSIX standard reserves a lot of identifiers that are likely to show up in normal code: (例如,POSIX标准保留了很多可能以普通代码显示的标识符:)
While using these names for your own purposes right now might not cause a problem, they do raise the possibility of conflict with future versions of that standard.
(虽然现在将这些名称用于您自己的目的可能不会引起问题,但它们确实增加了与该标准的未来版本冲突的可能性。)
Personally I just don't start identifiers with underscores.
(我个人只是不以下划线开头标识符。)
New addition to my rule: Don't use double underscores anywhere, which is easy as I rarely use underscore. (我的规则的新增内容:不要在任何地方使用双下划线,这很容易,因为我很少使用下划线。)
After doing research on this article I no longer end my identifiers with _t
as this is reserved by the POSIX standard.
(在对本文进行研究之后,我不再用_t
结束标识符,因为POSIX标准保留了该标识符。)
The rule about any identifier ending with _t
surprised me a lot.
(关于以_t
结尾的任何标识符的规则使我感到非常惊讶。)
I think that is a POSIX standard (not sure yet) looking for clarification and official chapter and verse. (我认为这是POSIX标准(尚不确定),需要澄清和官方章节。)
This is from the GNU libtool manual , listing reserved names. (这来自GNU libtool手册 ,其中列出了保留名称。)
CesarB provided the following link to the POSIX 2004 reserved symbols and notes 'that many other reserved prefixes and suffixes ... can be found there'.
(CesarB提供了指向POSIX 2004保留符号的以下链接,并指出“在此可以找到许多其他保留的前缀和后缀...”。)
The POSIX 2008 reserved symbols are defined here. (此处定义了POSIX 2008保留符号。)
The restrictions are somewhat more nuanced than those above. (这些限制比上述限制更加细微。)