r/cpp_questions • u/Equivalent_Union2768 • 9d ago
OPEN cpp class vs namespace
in my personal opinion, at least half of the purpose of using nested namespace is to manage visibility, the rust's mod allows it but cpp's namespace does not, only class allows it( the public and private)
is using classes with static functions inside everywhere instead of using namespace an already existing way to use the "namespace" in a way that i can more conveniently manage visibility?
3
Upvotes
View all comments
3
u/mredding 9d ago
Namespaces in C++ partition the global declarative region to prevent symbol collisions, control name lookup rules, and manage linkage and ABI versioning.
The global namespace spans all Translation Units, so at best, a symbol collision can lead to a declaration error, or at worst, you can get a silent ODR violation. Namespaces allow symbols to maintain their global linkage with a unique prefix.
ADL allows for unqualified name lookups. This is how the stream operators
<<and>>are correctly resolved for streamable types. When ADL exhausts the current lexical scope, it then proceeds to search the namespces associated with the parameter types. This allows several template static-polymorphism idioms like:For types of
aandb, there may be a specialization associated with these types. That will be matched first. If an sufficient match is not found, the compiler defaults tostd::swap. If you wanted to usestd::swapexplicitly, the code could name it explicitly. If you don't want a type specific match with no fallback, you can exclude theusingdeclaration.ADL is central to C++ and you trigger it all the time. While
std::swapdisables ADL forswap, the compiler still needs to find the unqualifiedstdnamespace, unless you go full-pedantic and write::std::swap. A core principle of any programming language is to control what is in scope, reduce it to only what is usable and needed. So having nested namespaces, customization objects, and yes - classes for hidden friends, can reduce the symbol set the compiler must consider, which means less work, less compile time, and less opportunity for error.Namespaces can be composed to implement API versioning and decoupling the physical header layout from the logical API surface. A most basic example of this is:
You would also pull from nested
detailnamespaces to compose an API, and there are other techniques where you can layer namespaces so you bring unchanged implementation from older versions forward while replacing only a selection in the newer version.If you want to keep implementation details private, use an unnamed or anonymous namespace within a translation unit:
All symbols within have no external linkage. Unlike
staticwhich can only be put onto free functions to internalize their linkage, the anonymous namespace can be applied to whole user defined types. You CAN write these into headers, but it's not recommended, because every source file that includes it will independently compile those versions in, often leading to object code bloat and unnecessarily fat target binaries.Classes play a very tiny role in scope resolution, and has nothing to do with visibility. Access control does not limit visibility, since
privatemembers are still published in the header you've defined the class. You need to use other idioms to implement privacy, like opaque pointers, opaque types, incomplete types, and compiler barriers aka compiler firewalls, and that's going to span header and source files and require other redirections.