`namespace` — C++ Keyword
`namespace` — C++ Keyword
The namespace keyword in C++: groups declarations to avoid name collisions.
`namespace` — C++ Keyword
The namespace keyword in C++: groups declarations to avoid name collisions.
Use reference pages to confirm names, categories, nearby facilities, and the constraints that matter before writing or reviewing code.
namespaceGroups declarations under a named scope, preventing name collisions between libraries and modules.
namespace name { declarations }
namespace name::nested { declarations } // C++17 nested namespace shorthand
namespace { declarations } // unnamed (anonymous) namespace
#include <print>
namespace math {
double pi = 3.14159265358979;
double circle_area(double r) {
return pi * r * r;
}
}
// C++17 nested namespace shorthand
namespace app::ui {
void render() { std::println("rendering"); }
}
// Inline namespace (transparent to outer namespace)
namespace lib {
inline namespace v2 {
void func() { std::println("lib v2"); }
}
}
int main() {
std::println("{:.4f}", math::circle_area(5.0));
app::ui::render();
lib::func(); // resolves to lib::v2::func
}
static at file scope).inline namespace facilitates ABI versioning: older code using lib::func still compiles.using namespace in headers leaks names into every including translation unit — avoid it.namespaceint main() {
// Pick one facility from this reference page.
// Write the smallest program that exercises its main precondition,
// complexity rule, or lifetime constraint before scaling up.
return 0;
}