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

1

u/alfps 15d ago

Some libraries, notably (as I recall) the Xerces XML parser, use classes as a kind of non-extensible namespaces.

It was a 1990's thing. I would not recommend that for new code. Still in some cases it would be desirable with a non-extensible namespace, e.g. one called ascii that provides symbolic constants, and though it can be emulated with a class it feels very wrong to abuse a feature that way. Using class instead of namespace is not good communication. It indicates the wrong thing to a reader. And so I miss a final namespace feature. Alas, there has apparently not even been a proposal in that direction.

For visibility with namespaces one simply relies on some naming convention, much like in the Python language.

The Boost library convention is that private parts are in nested detail namespaces.

I often call them impl instead.