C++ Programming/Programming Languages/C++/Code/Statements/Scope/Namespaces

using namespaces

 * using

This using-directive indicates that any names used but not declared within the program should be sought in the ‘standard (std)'.

To make a single name from a available, the following using-declaration exists:

After this declaration, the name bar can be used inside the current instead of the more verbose version foo::bar. Note that programmers often use the terms declaration and directive interchangeably, despite their technically different meanings.

It is good practice to use the narrow second form (using declaration), because the broad first form (using directive) might make more names available than desired. Example:

In that case the declaration using foo::bar; would have made only foo::bar available, avoiding the clash of pi and foo::pi. This problem (the collision of identically-named variables or functions) is called "namespace pollution" and as a rule should be avoided wherever possible.

<tt>using</tt>-declarations can appear in a lot of different places. Among them are:


 * namespaces (including the default namespace)
 * functions

A <tt>using</tt>-declaration makes the name (or ) available in the scope of the declaration. Example:

Namespaces are hierarchical. Within the hypothetical <tt>food::fruit</tt>, the identifier <tt>orange</tt> refers to <tt>food::fruit::orange</tt> if it exists, or if not, then <tt>food::orange</tt> if that exists. If neither exist, <tt>orange</tt> refers to an identifier in the default.

Code that is not explicitly declared within a is considered to be in the default namespace.

Another property of namespaces is that they are open. Once a is declared, it can be redeclared (reopened) and  members can be added. Example:

Namespaces are most often used to avoid naming collisions. Although namespaces are used extensively in recent C++ code, most older code does not use this facility. For example, the entire standard library is defined within <tt>std</tt>, and in earlier standards of the language, in the default namespace.

For a long namespace name, a shorter alias can be defined (a alias declaration). Example:

There exists a special : the unnamed namespace. This is used for names which are private to a particular source file or other :

In the surrounding scope, members of an unnamed namespace can be accessed without qualifying, i.e. without prefixing with the namespace name and <tt>::</tt> (since the namespace doesn't have a name). If the surrounding scope is a, members can be treated and accessed as a member of it. However, if the surrounding scope is a file, members cannot be accessed from any other source file, as there is no way to name the file as a scope. An unnamed namespace declaration is semantically equivalent to the following construct

where <tt>$$$</tt> is a unique identifier manufactured by the compiler.

As you can nest an unnamed namespace in an ordinary, and vice versa, you can also nest two unnamed namespaces.

Because of space considerations, we cannot actually show the command being used properly: it would require a very large program to show it working usefully. However, we can illustrate the concept itself easily.

We will examine the code moving from the start down to the end of the program, examining fragments of it in turn.

This just includes the iostream library so that we can use <tt>std::cout</tt> to print stuff to the screen.

We create a namespace called first and add to it two variables, first1 and x. Then we close it. Then we create a new namespace called second and put two variables in it: second1 and x. Then we re-open the first and add another variable called first2 to it. A can be re-opened in this manner as often as desired to add in extra names.

The first line of the main program is commented out because it would cause an error. In order to get at a name from the first, we must qualify the variable's name with the name of its before it and two colons; hence the second line of the main program is not a syntax error. The name of the variable is in scope: it just has to be referred to in that particular way before it can be used at this point. This therefore cuts up the list of global names into groups, each group with its own prefixing name.

The third line of the main program introduces the using namespace command. This commands pulls all the names in the first into scope. They can then be used in the usual way from there on. Hence the fourth and fifth lines of the program compile without error. In particular, the variable x is available now: in order to address the other variable x in the second, we would call it second::x as shown in line six. Thus the two variables called x can be separately referred to, as they are on the fifth and sixth lines.

We then pull the declarations in the called second in, again with the using namespace command. The line following is commented out because it is now an error (whereas before it was correct). Since both namespaces have been brought into the global list of names, the variable x is now ambiguous, and needs to be talked about only in the qualified manner illustrated in the ninth and tenth lines.

The eleventh line of the main program shows that even though first2 was declared in a separate section of the called first, it has the same status as the other variables in  first. A can be re-opened as many times as you wish. The usual rules of scoping apply, of course: it is not legal to try to declare the same name twice in the same.

There is a defined in the computer in special group of files. Its name is std and all the system-supplied names, such as cout, are declared in that in a number of different files: it is a very large namespace. Note that the #include statement at the very top of the program does not fully bring the in: the names are there but must still be referred to in qualified form. Line twelve has to be commented out because currently the system-supplied names like cout are not available, except in the qualified form std::cout as can be seen in line thirteen. Thus we need a line like the fourteenth line: after that line is written, all the system-supplied names are available, as illustrated in the last line of the program. At this point we have the names of three incorporated into the program.

As the example program illustrates, the declarations that are needed are brought in as desired, and the unwanted ones are left out, and can be brought in in a controlled manner using the qualified form with the double colons. This gives the greater control of names needed for large programs. In the example above, we used only the names of variables. However, namespaces also control, equally, the names of procedures and classes, as desired.