CKTIS / C++
namespace — namespaces
The namespace keyword defines a namespace or a namespace alias. Namespaces group declarations and help distinguish identically named entities; qualified access takes the form N::name.
Version: C++23; repository compiler flag, production runtime not attested
Syntax
namespace N { /* declarations */ }
namespace A::B { /* declarations */ }
namespace Alias = A::B;
namespace { /* declarations */ }
inline namespace V { /* declarations */ }
using namespace N;namespace N { ... } groups declarations of functions, types, and variables in namespace N. N::name identifies a member. A namespace is neither a class nor an object. Another namespace N { ... } definition in the appropriate scope extends the same namespace rather than replacing it.
Namespace definitions are allowed only at namespace scope, including global scope—not inside functions or classes. namespace A::B { ... } defines nested namespaces. namespace Alias = A::B; creates another name for an existing namespace, not a copy of its members.
namespace { ... } defines an unnamed namespace that is distinct for each translation unit; it is not the global namespace. inline namespace V { ... } allows its members to be used in most respects as though they belonged to the immediately enclosing namespace.
using namespace N; is a directive affecting name lookup, not a definition or alias. It can cause ambiguity; explicit qualification reduces that risk. The example extends app::math and uses the alias calc. The syntax covered is within C++23; the supplied source document is an evolving working draft, not a frozen C++23 edition.
Examples
#include <iostream>
namespace app::math {
int twice(int value) { return value * 2; }
}
namespace app::math {
constexpr int input = 7;
}
namespace calc = app::math;
int main() {
std::cout << calc::twice(calc::input) << '\n';
}
Result
14
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.