Namespaces in C++
Namespaces partition the global name pool so libraries coexist: std::vector vs boost::vector never collide because the qualifier disambiguates. Every standard name lives in std precisely so user code owns the unqualified ground.
using namespace std; in a header sprays thousands of names into every includer — banned by every style guide. Prefer full qualification, targeted using-declarations (using std::string;), or namespace aliases (namespace fs = std::filesystem;) for long nests. Anonymous namespaces give file-local linkage, the modern static.
Qualify to disambiguate
std::sort vs my::sort coexist peacefully — the :: prefix selects exactly which entity is meant.
Never using-directive in headers
using namespace std; at header scope forces itself on all includers transitively — keep it out of headers, period.
Targeted using-declarations
using std::string; imports one name into the current scope — convenience without the collision blast radius.
Aliases and anonymous
namespace fs = std::filesystem; shortens deep nests; anonymous namespace { ... } hides helpers per translation unit.
#include <iostream>#include <string> namespace app { std::string tag = "v1"; void show() { std::cout << tag << std::endl; }} int main() { app::show(); return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.