Why parts of the standard library in a separate namespace

Why are some parts of the C ++ standard library (and it seems that in later standards it is becoming more common) not directly in the ::std , but rather in a nested namespace? std::chrono and std::filesystem are two examples that may be useful.

+7
c ++
source share
3 answers

The obvious reason is the same as for any other project: without it there will be name collisions. For example, std::filesystem::copy versus std::copy .

This is not a complete explanation because

  • I cannot immediately see any collisions in the std::chrono
  • the committee could simply choose a non-conflict name instead

More convincing

  • These libraries are based on their predecessors Boost because they turned out to be useful and well tested. This means that there is existing code using them, and it is easier to port this code to C ++ 11 if the structure of the namespace does not change and no new conflicts are introduced.
  • In a broader sense, C ++ best practices have evolved since the first version of the standard library.

Please note that (as indicated in the default note) the regex library chooses consistency with the best practices of Boost over namespace, so it seems that # 1 is more important. The same is true for std::thread , etc.


Separating the factual from speculative and hypothetical:

  • Discussed libraries are based on predecessors Boost
  • The libraries in question preserve the namespace structure of their predecessors Boost
  • The namespace structures of the libraries under discussion are incompatible with each other (or with the rest of the standard library)
  • Changing the structure of the C ++ namespace, even without name collisions, can have ADL-related side effects (so this is not a trivial search and replacement)

Conclusion: the namespace structure was chosen for consistency with Boost, and not in accordance with the rest of the standard library, or even among the libraries added in C ++ 11.

+9
source share

Having multiple namespaces minimizes the likelihood of problems with an alias. Everything from the standard library should be within std:: , ok, but the standard library is still quite large for a single namespace. For example, in std::filesystem you have the function std::filesystem::status . It is easy to imagine other parts of the library where the status function may be needed at some point. And the namespace also gives a clearer purpose or β€œdomain” function when reading code.

Also, if you, for example, using namespace std; at some point in your code, you may have some kind of status function in the current namespace, so this will allow you not to repeat std:: without pulling every thing from the standard library directly into the local namespace or more selectively select a specific subset the names you want to make.

However, breaking an API into uncountable namespaces is also not too practical. Perhaps someone more familiar with the C ++ standardization process can give a more complete answer with details on how the committee decides when to add the namespace, but in the end it is a question of design and style, and there are no unambiguous, undeniable rules .

0
source share

Consider std :: filesystem :: absolute .

Without the filesystem subspace, this function should have been called something like std::filesystem_absolute . This is a viable approach, but less flexible than one with a subquery. Remember that you can use namespace aliases .

it seems that in later standards it is becoming more common

I think this is so because the standard library is becoming more and more large.

0
source share

All Articles