Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reserved names in the global namespace

Arising from my answer to Dynamic array of objects in C++ and as a follow up to What are the rules about using an underscore in a C++ identifier?: apparently, names beginning with _ followed by an uppercase letter are reserved in the global namespace.

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

In my answer to the first question I had a class that looked like this

class A
{
 private:
   vector<int> _Ints;
}

In the comments I was told the _Ints identifier invokes undefined behavior, since it is a reserved name. However, according to the recent draft of the standard, name look-up of a member variable occurs using the following rule:

3.4.3.1 Class members [class.qual]

If the nested-name-specifier of a qualified-id nominates a class, the name specified after the nested-namespecifier is looked up in the scope of the class (10.2), except for the cases listed below. The name shall represent one or more members of that class or of one of its base classes

To me, that means that no member variable can ever be part of the global namespace, as its scope is the class.

And now, the question:

Is my understanding correct that member variables will never violate the implementation reserved names rule since they are not in the global namespace? If I am not correct, could someone explain my misunderstanding of the look-up rule?

like image 830
rerun Avatar asked Mar 12 '12 14:03

rerun


People also ask

What is an example of a named Namespace?

An example of this is the std namespace which is declared in each of the header files in the standard library. Members of a named namespace can be defined outside the namespace in which they are declared by explicit qualification of the name being defined.

How do I refer to the global namespace in Python?

You can use the :: qualifier to refer to the global namespace by using the predefined identifier global. For example, in the following program, a class called MyClass is declared in both the Counter namespace and in the global namespace. To access the version of MyClass in the global namespace, the predefined alias global is used.

How to access the members of a namespace?

Identifiers outside the namespace can access the members by using the fully qualified name for each identifier, for example std::vector<std::string> vec;, or else by a using Declaration for a single identifier (using std::string), or a using Directive for all the identifiers in the namespace (using namespace std;).

What if I don't declare a namespace for my program?

If you don't declare a namespace for your program, then the default global namespace is used. You can use the :: qualifier to refer to the global namespace by using the predefined identifier global. For example, in the following program, a class called MyClass is declared in both the Counter namespace and in the global namespace.


3 Answers

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

like image 91
James Kanze Avatar answered Sep 22 '22 19:09

James Kanze


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.

like image 44
interjay Avatar answered Sep 23 '22 19:09

interjay


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

like image 28
Konrad Rudolph Avatar answered Sep 20 '22 19:09

Konrad Rudolph