The Rationale Behind Namespaces

The Rationale Behind Namespaces

Some users consider the addition of namespaces to C++ as superfluous or even harmful. In order to understand why namespaces were added to the language, imagine that the file system on your PC / Mainframe didn’t have any directories and subdirectories. All files would be stored in a flat repository, which would be visible to every user and application at all times. Consequently, you would run into extreme difficulties. Filenames would clash (with some systems limiting a filename to 8 characters + 3 for the extension, this is likely to happen); simple actions like listing, copying or searching files would be much more difficult; and security and authorization restrictions would be severely compromised.

Namespaces in C++ are equivalent to directories. They can be nested easily, they protect you from name clashes, they allow you to hide declarations simply by not using a namespace, and they do not incur any runtime or memory overhead.


Share the Post: