Advertisement
Beginner C++, Namespaces

How do you nest namespaces, and what is the C++17 shorthand?

Short answer

Namespaces can be nested to reflect module structure. C++17 added a compact syntax using :: to declare nested namespaces in one line, avoiding multiple nested braces. Names are accessed with the full qualified path (outer::inner::name).

Save C++ mock interview
Advertisement
AModel answer
Namespaces can be nested to reflect module structure. C++17 added a compact syntax using :: to declare nested namespaces in one line, avoiding multiple nested braces. Names are accessed with the full qualified path (outer::inner::name).
namespace a::b::c {   // C++17 nested shorthand
    int x;
}
int y = a::b::c::x;
Real-world example
A project uses nested namespaces like company::product::detail to organize its code.
Likely follow-ups
  • What did C++17 simplify? | How do you access a deeply nested name?
Related topics