Namespaces
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
#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;
}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
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.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
targeted-using.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
// 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
static keyword.anonymous-namespace.cpp
namespace {
int internalCounter = 0; // only visible in this .cpp file
void helper() {
++internalCounter;
}
}
int main() {
helper();
return 0;
}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::memberis 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.