Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

c++ - 在C ++标识符中使用下划线的规则是什么?(What are the rules about using an underscore in a C++ identifier?)

It's common in C++ to name member variables with some kind of prefix to denote the fact that they're member variables, rather than local variables or parameters.

(在C ++中,通常使用某种前缀来命名成员变量,以表示它们是成员变量,而不是局部变量或参数。)

If you've come from an MFC background, you'll probably use m_foo .

(如果您来自MFC背景,则可能会使用m_foo 。)

I've also seen myFoo occasionally.

(我也偶尔见过myFoo 。)

C# (or possibly just .NET) seems to recommend using just an underscore, as in _foo .

(C#(或者可能只是.NET)似乎建议仅使用下划线,例如_foo 。)

Is this allowed by the C++ standard?

(C ++标准允许吗?)

  ask by Roger Lipscombe translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

(某些名称和函数签名集始终保留给实现:)

  • Each name that contains a double underscore ( __ ) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the implementation for any use.

    (每个包含双下划线( __ )或以下划线后跟大写字母(2.11)开头的名称都保留给实现以供任何使用。)

  • Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.

    (每个以下划线开头的名称都保留给实现,以用作全局名称空间中的名称。)

    165

    (165)

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)具有外部链接的保留标识符的列表包括errnomath_errhandlingsetjmpva_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标准保留了很多可能以普通代码显示的标识符:)

  • Names beginning with a capital E followed a digit or uppercase letter:

    (以大写E开头的名称后跟数字或大写字母:)

    • may be used for additional error code names.

      (可以用于其他错误代码名称。)

  • Names that begin with either is or to followed by a lowercase letter

    (以isto开头的小写字母)

    • may be used for additional character testing and conversion functions.

      (可以用于其他字符测试和转换功能。)

  • Names that begin with LC_ followed by an uppercase letter

    (以LC_开头的名称,后跟一个大写字母)

    • may be used for additional macros specifying locale attributes.

      (可以用于指定区域设置属性的其他宏。)

  • Names of all existing mathematics functions suffixed with f or l are reserved

    (保留所有以fl结尾的现有数学函数的名称)

    • for corresponding functions that operate on float and long double arguments, respectively.

      (对于分别对float和long double参数运行的相应函数。)

  • Names that begin with SIG followed by an uppercase letter are reserved

    (保留以SIG开头且后跟大写字母的名称)

    • for additional signal names.

      (有关其他信号名称。)

  • Names that begin with SIG_ followed by an uppercase letter are reserved

    (保留以SIG_开头,大写字母的名称)

    • for additional signal actions.

      (用于其他信号动作。)

  • Names beginning with str , mem , or wcs followed by a lowercase letter are reserved

    (保留以strmemwcs开头的名称,后跟小写字母)

    • for additional string and array functions.

      (用于其他字符串和数组函数。)

  • Names beginning with PRI or SCN followed by any lowercase letter or X are reserved

    (保留以PRISCN开头的名称,后跟任何小写字母或X)

    • for additional format specifier macros

      (用于其他格式说明符宏)

  • Names that end with _t are reserved

    (以_t结尾的名称被保留)

    • for additional type names.

      (用于其他类型名称。)

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.

(这些限制比上述限制更加细微。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...