Didn"t forge 2008-10-23 07:02 采纳率: 25%
浏览 219
已采纳

在 c + + 标识符中使用下划线的规则是什么?

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# (or possibly just .NET) seems to recommend using just an underscore, as in _foo. Is this allowed by the C++ standard?

转载于:https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier

  • 写回答

5条回答 默认 最新

  • larry*wei 2008-10-25 20:22
    关注

    The rules (which did not change in 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. (You are allowed to add template specializations, though.)

    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:

    • 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) Such names are also reserved in namespace ::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:

    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.

    • 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).
    • 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
    • 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.

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

    154) The list of reserved identifiers with external linkage includes errno, math_errhandling, setjmp, and 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:

    • Names beginning with a capital E followed a digit or uppercase letter:
      • may be used for additional error code names.
    • Names that begin with either is or to followed by a lowercase letter
      • may be used for additional character testing and conversion functions.
    • Names that begin with LC_ followed by an uppercase letter
      • may be used for additional macros specifying locale attributes.
    • Names of all existing mathematics functions suffixed with f or l are reserved
      • for corresponding functions that operate on float and long double arguments, respectively.
    • Names that begin with SIG followed by an uppercase letter are reserved
      • for additional signal names.
    • Names that begin with SIG_ followed by an uppercase letter are reserved
      • for additional signal actions.
    • Names beginning with str, mem, or wcs followed by a lowercase letter are reserved
      • for additional string and array functions.
    • Names beginning with PRI or SCN followed by any lowercase letter or X are reserved
      • for additional format specifier macros
    • Names that end with _t are reserved
      • 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.

    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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据