Namespaces
Why Namespaces Exist (Name Collisions)
A namespace is a named space in a program, containing a group of related identifiers (functions, variables, types, etc.). Everything that is declared inside a n…
Declaring and Defining Namespaces
The simplest way to create a namespace is to simply write down the keyword namespace, give it a name, and then open a pair of curly braces. Everything declared…
Nested Namespaces
Sometimes one namespace isn’t enough. You create a very broad namespace like Graphics, but then you start creating code for drawing, color management, and matri…
using Declarations
Typing the fully qualified name all the time soon gets boring. After a few calls to Graphics::Color::setRed or Company::Project::Utils::add, the repeated prefix…
using Directives
A using directive is the overbearing cousin of a using declaration. The declaration pulls in one well chosen name; the directive, every name to be found within…
Namespace Aliases
Long nested names are correct, but they are also tiring to type and tiring to read. When the call looks like Company::Graphics::Rendering::Shaders::load(), then…
Unnamed (Anonymous) Namespaces
Occasionally, you may require a name that belongs strictly within a single file and must never be made accessible to any other files. A global function or varia…
The Global Namespace
Every name within a C++ program must live somewhere. When a function or variable is written and not included within some namespace declaration block, it does no…
Why using namespace std; Should Be Avoided
One line can discreetly decide how many names qualify a search for each proceeding name. That line is using namespace std;. It's a simple and innocent line that…