CppNamespaces

Namespaces

As a program grows — or pulls in multiple libraries — it becomes likely that two pieces of code want to use the same name for different things. Two libraries might both define a class called Logger, or a function called process(). Without any way to separate them, the names collide and the program fails to compile. Namespaces solve this by giving names a "last name" — a scope they belong to.
Declaring a namespace

namespace-basics.cpp

CPP
#include <iostream>

namespace MyLib {
  void greet() {
    std::cout << "Hello from MyLib!\n";
  }

  int version = 2;
}

int main() {
  MyLib::greet();                       // "Hello from MyLib!"
  std::cout << MyLib::version << "\n";  // 2
  return 0;
}
Anything declared inside namespace MyLib { ... } is accessed from outside using the scope resolution operator: MyLib::greet(). Two different libraries can each define their own greet() without conflict, as long as they live in different namespaces.
using namespace — convenient, but risky
Typing a namespace prefix on every call gets tedious, which is why using namespace MyLib; exists — it brings every name from MyLib into the current scope unqualified. You have already seen this with using namespace std;, sometimes recommended in beginner examples for cin/cout.
Avoid using namespace at global or header scope
A blanket using namespace X; at global scope — or worse, inside a header file — pollutes the global namespace for every file that includes it. It defeats the entire purpose of namespaces: names from different libraries can collide again, and it becomes unclear where a given function actually came from when reading the code. This is the same reason the "Your First Program" page avoids using namespace std; at the top of the file.
A safer alternative: targeted using declarations
Instead of importing an entire namespace, bring in only the specific names you actually use with a using declaration. This keeps the convenience of an unqualified name without opening the door to everything else in the namespace.

targeted-using.cpp

CPP
#include <iostream>

namespace MyLib {
  void greet() { std::cout << "Hi!\n"; }
  void shutdown() { std::cout << "Bye!\n"; }
}

using MyLib::greet; // only this name is brought in

int main() {
  greet();          // fine, no prefix needed
  MyLib::shutdown(); // still needs the prefix
  return 0;
}
Nested namespaces

Namespaces can nest inside each other to model a hierarchy, such as a company name containing a module name. Traditionally this required nested braces; C++17 added a shorthand for writing it in one line.

nested-namespaces.cpp

CPP
// Traditional nesting
namespace Company {
  namespace Networking {
    void connect() { /* ... */ }
  }
}

// C++17 shorthand — equivalent to the above
namespace Company::Networking {
  void disconnect() { /* ... */ }
}

int main() {
  Company::Networking::connect();
  Company::Networking::disconnect();
  return 0;
}
Anonymous namespaces
A namespace with no name gives its contents internal linkage — meaning those names are only visible within the current file (the current translation unit) and cannot clash with identically named symbols in other files. It's a modern, more C++-idiomatic replacement for marking file-local helpers with the old static keyword.

anonymous-namespace.cpp

CPP
namespace {
  int internalCounter = 0; // only visible in this .cpp file

  void helper() {
    ++internalCounter;
  }
}

int main() {
  helper();
  return 0;
}
Rule of thumb
Prefer explicit MyLib:: qualification or targeted using declarations inside function bodies. Reserve a broad using namespace for short-lived scopes, like inside a single function, and never at global or header scope.
  • Namespaces prevent name collisions between independently developed code.

  • Namespace::member is the fully qualified way to reach a name.

  • using namespace X; at global/header scope pollutes every including file — avoid it there.

  • using X::member; imports just one name — safer and still convenient.

  • C++17 lets you write namespace A::B::C { } instead of nesting braces manually.

  • Anonymous namespaces give file-local (internal) linkage to their contents.