Namespaces in C++

By Alex Allain
One of C++'s less heralded additions is addition of namespaces, which can be used to structure a program into "logical units". A namespace functions in the same way that a company division might function -- inside a namespace you include all functions appropriate for fulfilling a certain goal. For instance, if you had a program that connected to the Internet, you might have a namespace to handle all connection functions:

namespace net_connect
  int make_connection();
  int test_connection();
  //so forth...
You can then refer to functions that are part of a namespace by prefixing the function with the namespace name followed by the scope operator -- ::. For instance,
By enabling this program structure, C++ makes it easier for you to divide up a program into groups that each perform their own separate functions, in the same way that classes or structs simplify object oriented design. But namespaces, unlike classes, do not require instantiation; you do not need an object to use a specific namespace. You only need to prefix the function you wish to call with namespace_name:: -- similar to how you would call a static member function of a class.

Another convenience of namespaces is that they allow you to use the same function name, when it makes sense to do so, to perform multiple different actions. For instance, if you were implementing a low-level IO routine and a higher level IO routine that uses that lower level IO, you might want to have the option of having two different functions named "input" -- one that handles low-level keyboard IO and one that handles converting that IO into the proper data type and setting its value to a variable of the proper type.

So far, when we've wanted to use a namespace, we've had to refer to the functions within the namespace by including the namespace identifier followed by the scope operator. You can, however, introduce an entire namespace into a section of code by using a using-directive with the syntax
using  namespace namespace_name;
Doing so will allow the programmer to call functions from within the namespace without having to specify the namespace of the function while in the current scope. (Generally, until the next closing bracket, or the entire file, if you aren't inside a block of code.) This convenience can be abused by using a namespace globally, which defeats some of the purpose of using a namespace. A common example of this usage is
using namespace std; 
which grants access to the std namespace that includes C++ I/O objects cout and cin.

Finally, you can introduce only specific members of a namespace using a using-declaration with the syntax
using namespace_name::thing;
One trick with namespaces is to use an unnamed namespace to avoid naming conflicts. To do so, simply declare a namespace with the normal syntax, but leave off the identifier; when this is done, you will have
and within the namespace you are assured that no global names will conflict because each namespace's function names take precedence over outside function names.

Now, you might ask, how can you actually use anything in that namespace? When your program is compiled, the "anonymous" namespace you have created will be accessible within the file you created it in. In effect, it's as though an additional "using" clause was included implicitly. This effectively limits the scope of anything in the namespace to the file level (so you can't call the functions in that namespace from another other file). This is comparable to the effect of the static keyword.

Renaming namespaces

Finally, if you just don't feel like typing the entire name of namespace, but you're trying to keep to a good style and not use the using keyword, you can rename a namespace to reduce the typing:
namespace <new> = <old>

Related articles
The many uses of the static keyword in C and C++