I was having a look through some open source C++ code and notice a lot of double under scores where used in the code, mainly at the start of variable names.
我查看了一些开放源码c++代码,注意到在代码中使用的分数下有很多双值,主要是在变量名的开头。
return __CYGWIN__;
Just wondering is there a reason for this, or is it just some people code styles? I would think that I makes it hard to read.
只是想知道这有什么原因,或者只是一些人编写样式?我认为我使它难以阅读。
6 个解决方案
#1
109
From Programming in C++, Rules and Recommendations :
从c++编程,规则和建议:
The use of two underscores (`__') in identifiers is reserved for the compiler's internal use according to the ANSI-C standard.
根据ANSI-C标准,标识符中使用两个下划线(“__”)是为编译器的内部使用而预留的。
Underscores (`_') are often used in names of library functions (such as "_main" and "_exit"). In order to avoid collisions, do not begin an identifier with an underscore.
下划线(' _')通常用于库函数的名称(如"_main"和"_exit")。为了避免冲突,不要以下划线开始标识符。
#2
46
Unless they feel that they are "part of the implementation", i.e. the standard libraries, then they shouldn't.
除非他们觉得自己是“实现的一部分”,即标准库,否则他们不应该这么做。
The rules are fairly specific, and are slightly more detailed than some others have suggested.
这些规则相当具体,而且比其他一些人建议的更详细。
All identifiers that contain a double underscore or start with an underscore followed by an uppercase letter are reserved for the use of the implementation at all scopes, i.e. they might be used for macros.
所有包含双下划线或以下划线开头、后跟大写字母的标识符都保留为在所有范围内使用实现,即它们可能用于宏。
In addition, all other identifiers which start with an underscore (i.e. not followed by another underscore or an uppercase letter) are reserved for the implementation at the global scope. This means that you can use these identifiers in your own namespaces or in class definitions.
此外,所有其他以下划线(即不加下划线或大写字母)开头的标识符都保留给全局范围的实现。这意味着您可以在自己的名称空间或类定义中使用这些标识符。
This is why Microsoft use function names with a leading underscore and all in lowercase for many of their core runtime library functions which aren't part of the C++ standard. These function names are guaranteed not to * with either standard C++ functions or user code functions.
这就是为什么微软使用函数名的一个主要的下划线,并且所有的核心运行时库函数都是小写的,而这些函数不是c++标准的一部分。这些函数名保证不会与标准c++函数或用户代码函数冲突。
#3
32
According to the C++ Standard, identifiers starting with one underscore are reserved for libraries. Identifiers starting with two underscores are reserved for compiler vendors.
根据c++标准,以下划线开头的标识符为库保留。以两个下划线开头的标识符是为编译器供应商保留的。
#4
9
The foregoing comments are correct. __Symbol__
is generally a magic token provided by your helpful compiler (or preprocessor) vendor. Perhaps the most widely-used of these are __FILE__
and __LINE__
, which are expanded by the C preprocessor to indicate the current filename and line number. That's handy when you want to log some sort of program assertion failure, including the textual location of the error.
以上评论是正确的。__Symbol__通常是由您的编译器(或预处理器)供应商提供的魔法令牌。其中最广泛使用的可能是__FILE__和__LINE__,它们由C预处理器扩展以指示当前文件名和行号。当您想要记录某种程序断言失败(包括错误的文本位置)时,这很方便。
#5
8
It's something you're not meant to do in 'normal' code. This ensures that compilers and system libraries can define symbols that won't collide with yours.
这是你在“正常”代码中不应该做的事情。这确保编译器和系统库可以定义不会与您的代码冲突的符号。
#6
1
In addition to libraries which many other people responded about, Some people also name macros or #define values for use with the preprocessor. This would make it easier to work with, and may have allowed bugs in older compilers to be worked around.
除了许多其他人响应的库之外,有些人还命名宏或#define值用于预处理器。这将使它更易于处理,并且可能使旧编译器中的bug得以处理。
Like others mentioned, it helps prevent name collision and helps to delineate between library variables and your own.
和前面提到的一样,它有助于防止名称冲突,有助于区分库变量和您自己的变量。
#1
109
From Programming in C++, Rules and Recommendations :
从c++编程,规则和建议:
The use of two underscores (`__') in identifiers is reserved for the compiler's internal use according to the ANSI-C standard.
根据ANSI-C标准,标识符中使用两个下划线(“__”)是为编译器的内部使用而预留的。
Underscores (`_') are often used in names of library functions (such as "_main" and "_exit"). In order to avoid collisions, do not begin an identifier with an underscore.
下划线(' _')通常用于库函数的名称(如"_main"和"_exit")。为了避免冲突,不要以下划线开始标识符。
#2
46
Unless they feel that they are "part of the implementation", i.e. the standard libraries, then they shouldn't.
除非他们觉得自己是“实现的一部分”,即标准库,否则他们不应该这么做。
The rules are fairly specific, and are slightly more detailed than some others have suggested.
这些规则相当具体,而且比其他一些人建议的更详细。
All identifiers that contain a double underscore or start with an underscore followed by an uppercase letter are reserved for the use of the implementation at all scopes, i.e. they might be used for macros.
所有包含双下划线或以下划线开头、后跟大写字母的标识符都保留为在所有范围内使用实现,即它们可能用于宏。
In addition, all other identifiers which start with an underscore (i.e. not followed by another underscore or an uppercase letter) are reserved for the implementation at the global scope. This means that you can use these identifiers in your own namespaces or in class definitions.
此外,所有其他以下划线(即不加下划线或大写字母)开头的标识符都保留给全局范围的实现。这意味着您可以在自己的名称空间或类定义中使用这些标识符。
This is why Microsoft use function names with a leading underscore and all in lowercase for many of their core runtime library functions which aren't part of the C++ standard. These function names are guaranteed not to * with either standard C++ functions or user code functions.
这就是为什么微软使用函数名的一个主要的下划线,并且所有的核心运行时库函数都是小写的,而这些函数不是c++标准的一部分。这些函数名保证不会与标准c++函数或用户代码函数冲突。
#3
32
According to the C++ Standard, identifiers starting with one underscore are reserved for libraries. Identifiers starting with two underscores are reserved for compiler vendors.
根据c++标准,以下划线开头的标识符为库保留。以两个下划线开头的标识符是为编译器供应商保留的。
#4
9
The foregoing comments are correct. __Symbol__
is generally a magic token provided by your helpful compiler (or preprocessor) vendor. Perhaps the most widely-used of these are __FILE__
and __LINE__
, which are expanded by the C preprocessor to indicate the current filename and line number. That's handy when you want to log some sort of program assertion failure, including the textual location of the error.
以上评论是正确的。__Symbol__通常是由您的编译器(或预处理器)供应商提供的魔法令牌。其中最广泛使用的可能是__FILE__和__LINE__,它们由C预处理器扩展以指示当前文件名和行号。当您想要记录某种程序断言失败(包括错误的文本位置)时,这很方便。
#5
8
It's something you're not meant to do in 'normal' code. This ensures that compilers and system libraries can define symbols that won't collide with yours.
这是你在“正常”代码中不应该做的事情。这确保编译器和系统库可以定义不会与您的代码冲突的符号。
#6
1
In addition to libraries which many other people responded about, Some people also name macros or #define values for use with the preprocessor. This would make it easier to work with, and may have allowed bugs in older compilers to be worked around.
除了许多其他人响应的库之外,有些人还命名宏或#define值用于预处理器。这将使它更易于处理,并且可能使旧编译器中的bug得以处理。
Like others mentioned, it helps prevent name collision and helps to delineate between library variables and your own.
和前面提到的一样,它有助于防止名称冲突,有助于区分库变量和您自己的变量。