Namespaces in C++
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
What is a namespace?
A namespace is a named box for related identifiers. Its job is to prevent name clashes: two libraries can each have a print() without conflict, because one is LibA::print and the other LibB::print. You choose with the scope resolution operator ::.
Defining a namespace
#include <iostream>
using namespace std;
namespace Math {
int square(int x) { return x * x; }
}
namespace Physics {
int square(int x) { return x * x * 2; } // different meaning
}
int main() {
cout << Math::square(4) << "\n"; // 16
cout << Physics::square(4) << "\n"; // 32
return 0;
}16
32
The using directive
using namespace X; brings all of X's names into scope; a using declaration brings just one:
using namespace Math; // all of Math using Physics::square; // just this one name
A using declaration (using X::name;) is safer than a whole directive, especially in headers.
The std namespace
The entire standard library lives in std — that is why it is std::cout, std::vector, std::string. Writing using namespace std; lets you drop the prefix, handy in small programs but risky in large ones or in headers.
Nested namespaces
namespace App {
namespace UI {
void draw() {}
}
}
// call: App::UI::draw();
// C++17 shortcut: namespace App::UI { void draw() {} }Anonymous namespaces
A namespace with no name keeps its contents private to the current file — the modern replacement for file-scope static:
namespace {
int helper() { return 42; } // visible only in this file
}Common mistakes
using namespace std;in a header, leaking it into every file that includes it.- Two using directives that import clashing names, causing ambiguity.
- Forgetting the
::when a name lives in a namespace. - Overusing broad directives instead of targeted declarations.
Create two namespaces Metric and Imperial, each with a distance() function returning different units. Call both with the scope operator, then bring one in with a using declaration.
Summary
- Namespaces group names to prevent clashes.
- Select a name with the scope resolution operator
::. using namespace X;imports all;using X::name;imports one.- The standard library lives in
std. - Anonymous namespaces give file-local (internal) linkage.
Frequently Asked Questions
What is a namespace in C++?
print as long as each is inside its own namespace, and you pick which one with the scope resolution operator.What does using namespace std do in C++?
using namespace std; brings all names from the standard library's std namespace into scope, so you can write cout instead of std::cout. It is convenient for small programs but can cause name clashes in larger code, where qualifying names or narrower using declarations are safer.What is the scope resolution operator in C++?
:: selects a name from a particular namespace or class, as in std::cout or MyLib::init(). It tells the compiler exactly which version of a name you mean when the same name exists in more than one scope.What is the difference between a using directive and a using declaration in C++?
using namespace X;, brings in every name from namespace X. A using declaration, using X::name;, brings in just one specific name. The declaration is more targeted and safer because it does not flood the current scope with everything from the namespace.What is an anonymous namespace in C++?
static keyword at file scope.Namespace क्या है?
Namespace संबंधित identifiers के लिए एक नामित डिब्बा है। इसका काम नाम टकराव रोकना है: दो libraries हर एक का print() बिना संघर्ष हो सकता है, क्योंकि एक LibA::print है और दूसरा LibB::print। आप scope resolution operator :: से चुनते हैं।
Namespace परिभाषित करना
#include <iostream>
using namespace std;
namespace Math {
int square(int x) { return x * x; }
}
namespace Physics {
int square(int x) { return x * x * 2; } // alag arth
}
int main() {
cout << Math::square(4) << "\n"; // 16
cout << Physics::square(4) << "\n"; // 32
return 0;
}16
32
using directive
using namespace X; X के सभी नाम scope में लाता है; एक using declaration केवल एक लाता है:
using namespace Math; // poora Math using Physics::square; // sirf yeh ek naam
using declaration (using X::name;) पूरे directive से सुरक्षित है, खासकर headers में।
std namespace
पूरी standard library std में रहती है — इसीलिए यह std::cout, std::vector, std::string है। using namespace std; लिखना आपको prefix छोड़ने देता है, छोटे programs में काम का पर बड़े में या headers में जोखिम भरा।
Nested namespaces
namespace App {
namespace UI {
void draw() {}
}
}
// call: App::UI::draw();
// C++17 shortcut: namespace App::UI { void draw() {} }Anonymous namespaces
बिना नाम का namespace अपनी सामग्री वर्तमान file तक निजी रखता है — file-scope static का आधुनिक विकल्प:
namespace {
int helper() { return 42; } // sirf is file me dikhta hai
}आम गलतियाँ
- Header में
using namespace std;, इसे हर file में रिसाते हुए जो इसे include करे। - दो using directives जो टकराते नाम import करें, ambiguity पैदा करते हुए।
- जब नाम namespace में हो तब
::भूलना। - लक्षित declarations के बजाय व्यापक directives का अति-इस्तेमाल।
दो namespaces Metric और Imperial बनाएँ, हर एक में अलग units लौटाने वाला distance() function हो। दोनों को scope operator से call करें, फिर एक को using declaration से लाएँ।
सारांश
- Namespaces नाम टकराव रोकने को नामों को समूहित करते हैं।
- Scope resolution operator
::से एक नाम चुनें। using namespace X;सब import करता है;using X::name;एक।- Standard library
stdमें रहती है। - Anonymous namespaces file-local (internal) linkage देते हैं।