Reserved names in the global namespace

南楼画角 提交于 2019-12-05 08:27:36

_Int clearly violates the first rule: “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.” “any use” means exactly what it says: it could be a predefined macro, or trigger some special behavior in the compiler, or anything else the compiler author wants. It doesn't matter where you use the name, if you use it, it's undefined behavior (unless the compiler documentation states otherwise).

More generally, historically at least, compilers have been rather lax, and a number of system headers have traditionally included macros with names starting with a single underscore followed by a lower case letter. It's probably best avoiding those as well. (Historically, even, there have been names without an underscore as well. I know I've had problems with the name linux becoming 1. No underscores in sight, but... There's not much you can do about this, however, except change the name when the conflict occurs.)

Even more generally, underscores don't show up that well in some fonts, and it's best avoiding them at either end of a symbol.

The rules you quoted from the standard state that an identifier starting with an underscore followed by an uppercase letter is reserved for any use, not just in the global namespace. So naming a member variable _Ints is not allowed.

Identifiers starting with an underscore which is not followed by an underscore or uppercase letter are reserved in the global namespace. So you are allowed to name a member variable _ints for example, but you can't have a global variable named _ints which is in the global namespace.

apparently _(Upercase Letter) is reserved in the global namespace.

No. It is reserved everywhere. Read 17.4.3.2.1 again:

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.

This doesn’t mention “global namespace” at all (the global namespace is only relevant in the subsequent rule).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!