Advertisement
Beginner C++, Namespaces

What is the global namespace and how do you refer to it?

Short answer

The global namespace is the outermost scope, containing names not placed in any namespace. You refer to a global name explicitly with a leading :: (e.g., ::func), which is useful to disambiguate from a same-named name in the current namespace.

Save C++ mock interview
Advertisement
AModel answer
The global namespace is the outermost scope, containing names not placed in any namespace. You refer to a global name explicitly with a leading :: (e.g., ::func), which is useful to disambiguate from a same-named name in the current namespace. Minimizing what lives in the global namespace reduces collisions.
::printf("hi");   // global-scope function explicitly
Real-world example
A leading :: disambiguates the global ::size from a member or namespace-scoped size.
Likely follow-ups
  • How do you access a global name explicitly? | Why limit global-namespace usage?
Related topics