r/cpp_questions • • 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

13 comments sorted by

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:

using std::swap;
swap(a, b);

For types of a and b, there may be a specialization associated with these types. That will be matched first. If an sufficient match is not found, the compiler defaults to std::swap. If you wanted to use std::swap explicitly, the code could name it explicitly. If you don't want a type specific match with no fallback, you can exclude the using declaration.

ADL is central to C++ and you trigger it all the time. While std::swap disables ADL for swap, the compiler still needs to find the unqualified std namespace, 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:

namespace v1 {}
namespace v2 {}

namespace current {
  using namespace v2;
}

You would also pull from nested detail namespaces 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:

using namespace {}

All symbols within have no external linkage. Unlike static which 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 private members 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.