GNU标准库命名约定 [英] GNU standard library naming conventions

查看:104
本文介绍了GNU标准库命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我查看GNU库的实现时(主要是libstdc ++),我可以看到命名中存在重复出现的模式.模板类型命名为_Tp,成员具有前缀_M_,某些标记具有前缀双下划线,依此类推.我试图查找有关命名约定的文档无济于事. GNU有一个样式指南,代码中也有遵循,但更像是该命名约定的子集.

您知道有关GNU gcc库实现的样式细节的任何文档吗?

谢谢.

解决方案

下划线不是编码约定",而是为了避免与用户定义的宏等发生名称冲突.

来自 https://www.gnu.org/软件/libc/manual/html_node/Reserved-Names.html (这实际上是针对libc的,但我认为它也适用于libstdc ++):

除了本手册中记录的名称外,保留名称 包括所有的外部标识符(全局函数和变量) 以下划线(_)和所有标识符开头,无论使用什么 以两个下划线或一个下划线开头,然后是一个 大写字母为保留名称.这样库和 头文件可以为内部文件定义函数,变量和宏 目的,而不会与用户程序中的名称发生冲突.

GNU网站还提供了有关更多保留名称的更多信息. 另请参见的答案问题.看起来C ++标准本身规定了命名约定.

更新:

OP所要求的信息似乎散布在不同的页面上.我将尝试总结以下最重要的观点:

首先,可以找到有关_T_M_之类名称的信息

类型名称和模板形式参数名称:_ [A-Z] [^ _].*

示例:_Helper _CharT _N

成员数据和函数名称:_M _.*

示例:_M_num_elements _M_initialize()

静态数据成员,常量和枚举:_S _.*

示例:_S_max_elements _S_default_value

进一步的挖掘使我进入了 libstdc ++的贡献页面,它说:

GNU C ++库是GCC的一部分,并遵循相同的发展 模型,因此适用于为GCC做出贡献的一般规则 .

通过上面的链接,您将进入GNU GCC贡献页面,该页面显示为(在编码标准下)

所有文稿必须符合GNU编码标准.有 还有一些关于GCC的其他编码约定;这些包括 文档和测试套件要求以及 代码格式.

不符合标准的提交将被退回 要求解决任何此类问题.为了帮助 您可以使用脚本编写补丁 contrib/check_GNU_style.sh .. >

这最终将导致 GCC编码约定,这是一个通用指南. /p>

我希望这会提供一些更好的信息.

When I'm looking at the implementation of GNU libraries (well, libstdc++ mostly), I can see that there are recurring patterns in naming. Template types are named _Tp, members have prepending _M_, some tokens have prepending double underscores, etc. I tried to find documentation on naming conventions to no avail. GNU has a styling guide, which is also followed in the code, but is more like a subset of this naming convention.

Do you know any documentation on styling specifics of GNU gcc library implementations?

Thanks in advance.

解决方案

The underscores are not a "coding convention" but rather there to avoid name clashes with user defined macros etc.

From https://www.gnu.org/software/libc/manual/html_node/Reserved-Names.html (this is actually for libc, but I assume it holds for libstdc++ as well):

In addition to the names documented in this manual, reserved names include all external identifiers (global functions and variables) that begin with an underscore (‘_’) and all identifiers regardless of use that begin with either two underscores or an underscore followed by a capital letter are reserved names. This is so that the library and header files can define functions, variables, and macros for internal purposes without risk of conflict with names in user programs.

The GNU website also provides more information on further reserved names. Also see the answer to this question. It looks like the C++ standard itself dictates the naming conventions.

Update:

The information requested by the OP seems to be a little scattered across different pages. I will try to summarize the most important points below:

First of all, information regarding names like _T or _M_ can be found here.

Excerpt:

For nonstandard names appearing in Standard headers, we are constrained to use names that begin with underscores. This is called "uglification". The convention is: [...]

Type names and template formal-argument names: _[A-Z][^_].*

Examples: _Helper _CharT _N

Member data and function names: _M_.*

Examples: _M_num_elements _M_initialize ()

Static data members, constants, and enumerations: _S_.*

Examples: _S_max_elements _S_default_value

Further digging lead me to the libstdc++ contributing page, where it says:

The GNU C++ Library is part of GCC and follows the same development model, so the general rules for contributing to GCC apply.

Following above link, you will reach the GNU GCC contributing page, where it reads (under Coding Standards)

All contributions must conform to the GNU Coding Standards. There are also some additional coding conventions for GCC; these include documentation and testsuite requirements as well as requirements on code formatting.

Submissions which do not conform to the standards will be returned with a request to address any such problems. To help with the preparation of patches you can use the script contrib/check_GNU_style.sh.

This will eventually lead to the GCC Coding Conventions, which is a general guideline.

I hope this provides some better information.

这篇关于GNU标准库命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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