C++ Namespaces
A namespace is a named area that groups related C++ identifiers, such as functions, classes, and variables. Namespaces help prevent name conflicts when different parts of a program use the same identifier.
You already use the standard library namespace when you write names such as std::cout, std::string, and std::vector.
Why Namespaces Matter
Large programs often combine code from many files, libraries, and teams. Without namespaces, two functions with the same name in the same scope would conflict. A namespace lets each group of code keep its own names.
The :: operator is called the scope resolution operator. It tells C++ which namespace a name comes from.
Creating a Namespace
Use the namespace keyword, followed by a name and a block. Names declared inside the block belong to that namespace.
#include <iostream>
#include <string>
namespace store {
double taxRate = 0.08;
double addTax(double price) {
return price + (price * taxRate);
}
std::string label() {
return "store";
}
}
namespace shipping {
double addTax(double price) {
return price + 2.50;
}
}
int main(void) {
std::cout << store::label() << std::endl;
std::cout << store::addTax(100.0) << std::endl;
std::cout << shipping::addTax(100.0) << std::endl;
return 0;
}
Output:
store
108
102.5
Both namespaces contain a function named addTax. The calls do not conflict because store::addTax and shipping::addTax are different qualified names.
Using Declarations
A using declaration brings one selected name into the current scope. This can make code shorter while still keeping control over which names are imported.
#include <iostream>
namespace math_tools {
int square(int value) {
return value * value;
}
int cube(int value) {
return value * value * value;
}
}
int main(void) {
using math_tools::square;
std::cout << square(5) << std::endl;
std::cout << math_tools::cube(3) << std::endl;
return 0;
}
Output:
25
27
Only square is brought into main. The program still uses math_tools::cube with a qualified name.
Namespace Aliases
A namespace alias gives a shorter name to a long namespace. This is common when namespaces are nested or deeply descriptive.
#include <iostream>
#include <string>
namespace company {
namespace reports {
std::string status() {
return "ready";
}
}
}
int main(void) {
namespace rpt = company::reports;
std::cout << rpt::status() << std::endl;
return 0;
}
Output:
ready
The alias rpt refers to company::reports. It does not create a copy of the namespace; it is only a shorter name for the same namespace.
Avoid Using namespace std
You may see using namespace std; in small examples. It imports every name from the standard library namespace into the current scope. That can cause confusing conflicts in larger programs.
For clear beginner code, prefer std::cout, std::string, and other qualified standard library names. If a name is used many times in a small function, a specific declaration such as using std::cout; is usually safer than importing the whole namespace.
Common Namespace Rules
- Use namespaces to group related library, application, or module names.
- Use
namespace_name::member_nameto access a name from a namespace. - Use namespace aliases for long namespace paths.
- Prefer specific
usingdeclarations over broadusing namespacedirectives. - Do not put your own code inside
namespace std.
Takeaway: namespaces keep names organized and prevent conflicts, especially as programs grow across many files and libraries.
