{
using Window::value2 ;
std::cout << value2 << std::endl ;
}
The usingdeclaration in f()hides the value2defined in the global namespace.
As mentioned before, a usingdeclarationgives you finer control over the names intro-
duced from a namespace. A usingdirectivebrings all names from a namespace into the
current scope. It is preferable to use a declaration over a directive because a directive
effectively defeats the purpose of the namespace mechanism. A declaration is more
definitive because you are explicitly identifying the names you want to introduce into a
scope. A usingdeclaration does not pollute the global namespace, as is the case with a
usingdirective (unless, of course, you declare all names found in the namespace). Name
hiding, global namespace pollution, and ambiguity all are reduced to a more manageable
level by using the usingdeclaration.
The Namespace Alias ..........................................................................................
A namespace aliasis designed to provide another name for a named namespace. An alias
provides a shorthand term for you to use to refer to a namespace. This is especially true
if a namespace name is very long; creating an alias can help cut down on lengthy, repeti-
tive typing. Consider the following code:
namespace the_software_company
{
int value ;
//...
}
the_software_company::value = 10 ;
namespace TSC = the_software_company ;
TSC::value = 20 ;
A drawback, of course, is that your alias might collide with an existing name. If this is
the case, the compiler catches the conflict and you can resolve it by renaming the alias.
The Unnamed Namespace ..................................................................................
An unnamed namespace is simply that—a namespace that does not have a name. A com-
mon use of unnamed spaces is to shield global data from potential name clashes between
object files and other translation units. Every translation unit has a unique, unnamed
namespace. All names defined within the unnamed namespace (within each translation
unit) can be referred to without explicit qualification. Listings 18.9a and 18.9b are exam-
ples of two unnamed namespaces found in two separate files.
652 Day 18