问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/50604557/gnu-standard-library-naming-conventions