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

查看:43
本文介绍了在 C++ 标识符中使用下划线的规则是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C++ 中,用某种前缀命名成员变量是很常见的,以表示它们是成员变量,而不是局部变量或参数.如果您有 MFC 背景,您可能会使用 m_foo.我也偶尔看到 myFoo.

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. If you've come from an MFC background, you'll probably use m_foo. I've also seen myFoo occasionally.

C#(或可能只是 .NET)似乎推荐只使用下划线,如 _foo.C++ 标准允许这样做吗?

C# (or possibly just .NET) seems to recommend using just an underscore, as in _foo. Is this allowed by the C++ standard?

推荐答案

规则(在 C++11 中没有改变):

The rules (which did not change in C++11):

  • 保留在任何范围内,包括用作实现宏:
    • 以下划线开头的标识符,后跟大写字母
    • 包含相邻下划线(或双下划线")的标识符
    • 以下划线开头的标识符

    来自 2003 C++ 标准:

    From the 2003 C++ Standard:

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

    17.4.3.1.2 Global names [lib.global.names]

    Certain sets of names and function signatures are always reserved to the implementation:

    • 每个包含双下划线 (__) 或以下划线开头后跟大写字母 (2.11) 的名称都保留给实现以供任何使用.
    • 每个以下划线开头的名称都保留给实现以用作全局命名空间中的名称.165
    • 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.
    • Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.165

    165) 此类名称也在名称空间 ::std (17.4.3.1) 中保留.

    165) Such names are also reserved in namespace ::std (17.4.3.1).

    因为 C++ 基于 C 标准 (1.1/2, C++03) 而 C99 是规范性参考 (1.2/1, C++03) 这些也适用,来自 1999 C 标准:

    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:

    每个标题声明或定义其相关子条款中列出的所有标识符,并且可选地声明或定义在其相关的未来库方向子条款中列出的标识符,以及始终保留用于任何用途或用作文件范围标识符的标识符.

    7.1.3 Reserved identifiers

    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.

    • 以下划线开头的所有标识符和大写字母或其他下划线始终保留用于任何用途.
    • 以下划线开头的所有标识符始终保留用作标识符在普通名称空间和标签名称空间中都具有文件作用域.
    • 以下任何子条款中的每个宏名称(包括未来的库如果包含任何关联的标头,则保留用于指定的用途;除非另有明确说明(见 7.1.4).
    • 在以下任何子条款中具有外部链接的所有标识符(包括未来图书馆方向)始终保留用作外部标识符链接.154
    • 在以下任何子条款中列出的具有文件范围的每个标识符(包括未来库方向)保留用作宏名称和标识符如果包含任何关联的标头,则同一名称空间中的文件范围.

    不保留其他标识符.如果程序声明或定义了一个标识符保留它的上下文(7.1.4 允许的除外),或定义保留标识符作为宏名称,行为未定义.

    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.

    如果程序删除了(用#undef)第一个标识符的任何宏定义上面列出的组,行为未定义.

    If the program removes (with #undef) any macro definition of an identifier in the first group listed above, the behavior is undefined.

    154) 带有外部链接的保留标识符列表包括errnomath_errhandlingsetjmp和<代码>va_end.

    154) The list of reserved identifiers with external linkage includes errno, math_errhandling, setjmp, and va_end.

    其他限制可能适用.例如,POSIX 标准保留了很多很可能出现在正常代码中的标识符:

    Other restrictions might apply. For example, the POSIX standard reserves a lot of identifiers that are likely to show up in normal code:

    • 以大写 E 开头的名称后跟数字或大写字母:
      • 可用于其他错误代码名称.
      • Names beginning with a capital E followed a digit or uppercase letter:
        • may be used for additional error code names.
        • 可用于额外的字符测试和转换功能.
        • 可用于指定语言环境属性的其他宏.
        • 分别用于对 float 和 long double 参数进行操作的相应函数.
        • 其他信号名称.
        • 用于其他信号操作.
        • 用于附加字符串和数组函数.
        • 用于其他格式说明符宏
        • 用于其他类型名称.

        虽然现在将这些名称用于您自己的目的可能不会造成问题,但它们确实增加了与该标准的未来版本发生冲突的可能性.

        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.

        在对本文进行研究后,我不再以 _t 结尾我的标识符因为这是 POSIX 标准保留的.

        After doing research on this article I no longer end my identifiers with _t as this is reserved by the POSIX standard.

        关于任何以 _t 结尾的标识符的规则让我很惊讶.我认为这是一个 POSIX 标准(还不确定),正在寻找澄清和官方章节.这是来自 GNU libtool 手册,列出了保留名称.

        The rule about any identifier ending with _t surprised me a lot. I think that is a POSIX standard (not sure yet) looking for clarification and official chapter and verse. This is from the GNU libtool manual, listing reserved names.

        CesarB 提供了以下链接到 POSIX 2004 保留符号和注意可以在那里找到许多其他保留的前缀和后缀......".这POSIX 2008 保留符号在此处定义.这些限制比上面的那些更微妙.

        CesarB provided the following link to the POSIX 2004 reserved symbols and notes 'that many other reserved prefixes and suffixes ... can be found there'. The POSIX 2008 reserved symbols are defined here. The restrictions are somewhat more nuanced than those above.

        这篇关于在 C++ 标识符中使用下划线的规则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆